$0 By peterj On 2016-12-11 13:44:31 In Science/Technologiez
Simple NES ASM RNG (6502 random number generator)
getting a simple computer like the 6502 to produce pseudo random numbers with an even distribution can be a challenge. Here's an idea that I came up with:
LDA seed
ASL A
ASL A
CLC
ADC seed
CLC
ADC #03
STA seed
What this essentially does is multiply a seed value by 5, then add some other number (in this case, 3). As long as the last number added is prime, it will cycle through all 256 possible values before repeating (including 0, unlike a LSFR). If you want even more randomness, you could have several different RNG subroutines set up that add different prime numbers to create different sequences for different things. I hope somebody finds this helpful.
LDA seed
ASL A
ASL A
CLC
ADC seed
CLC
ADC #03
STA seed
What this essentially does is multiply a seed value by 5, then add some other number (in this case, 3). As long as the last number added is prime, it will cycle through all 256 possible values before repeating (including 0, unlike a LSFR). If you want even more randomness, you could have several different RNG subroutines set up that add different prime numbers to create different sequences for different things. I hope somebody finds this helpful.