I am circling back to this. I have found some elegant algorithms for RNG in 6502, but I am struggling with the part about scaling. Is it truly a uniform distribution to simply AND the RNG output with the upper limit operand, as bruddogtecmo says? It seems reasonable and very efficient, but I would like to test it.
If the RNG output is uniform 0-255, then yes -- ASSUMING
you are ANDing all low bits with no gaps.
IE: This only works with power-of-2 ranges (AND with power-of-2 minus one)
AND #$3F ; <- Good -- $40 is a power of 2, $3F is power-of-2 minus one
AND #$3E ; <- BAD -- will not work, $3F is not a power-of-2
Basically you're just dropping the high bits.
Disch's scaling algorithm makes perfect sense algebraically, but doesn't dropping the low byte mean if the product of the upper limit operand and RNG output is less than 256, it will always scale as 0? It doesn't seem right.
Yes, output less than 256 would be zero. That is desired behavior.
You can plug in some numbers to see how it'd work. Say you want a number between 0-2
(all numbers in hex)
RNG output | multiply by 3 | drop low byte | odds of result
00 to 55 | 0000 to 00FF | 00 | 56 / 100
56 to AA | 0102 to 01FE | 01 | 55 / 100
AB to FF | 0201 to 02FD | 02 | 55 / 100
As you can see, you have SLIGHTLY higher odds of hitting the low numbers, but it's as uniform as possible. That bias towards lower numbers increases as your range gets higher. This is easy to see with something like a range of 0-254, where odds of getting zero becomes twice as likely as getting any other number.
Only way I can think of to get TRULY uniform numbers (in a non-power-of-2 range) would be to conditionally reroll the RNG on certain results. IE: if you want 0-254, then you'd reroll if the RNG produced 255. But that's tough to generalize in 6502 and would be time consuming.EDIT:
Nevermind, rerolling wouldn't even work. Since the pRNG is just a repeating sequence, this would just widen the window of the next value in the sequence. IE, rerolling on 255 would just improve the odds of getting whatever number follows 255 in the sequence, since there are now 2 ways to produce it.
Rerolling only works if the RNG is truly random -- like with actual dice or something.
So yeah.. I have no idea how you'd get perfectly uniform distribution here. So multiply-and-drop-low-byte approach is going to be as good as anything./EDITEDIT 2:
Or maybe I'm wrong and rerolling would
work? I don't know, I'm not confident in either answer. =x/EDIT 2
But in either case, I could use a suitable test environment to run these algorithms by the thousands and plot the output. Can someone recommend a good free utility?
You could whip something up in python, I suppose. There are even online python interpreters, so you don't even need to install it.
Here's the first one I found: https://repl.it/languages/python3
Dunno if you can use a plotting library with that, though, but you could print the results out as text easily enough. If you have Python installed then you can probably get the matplotlib library.