Random number generator (very fast - very basic)

From Retrosoftware

(Difference between revisions)
Jump to: navigation, search
(New page: Random number generator (very fast - very basic) I saw a nice 6502 random number generator this morning, but at thousands of cycles per call, I thought a faster one might be useful here. ...)
Line 24: Line 24:
</pre></tt>
</pre></tt>
-
See the forums for a BBC sample program and .ssd
+
See the forums [http://www.retrosoftware.co.uk/forum/viewtopic.php?f=73&t=640] for a BBC sample program, .ssd and discussion.

Revision as of 09:20, 29 June 2011

Random number generator (very fast - very basic)

I saw a nice 6502 random number generator this morning, but at thousands of cycles per call, I thought a faster one might be useful here.

This is about the fastest random number generator you can get although it is really just a sequence of 256 numbers using 14 bytes (code and data if seed is in ZP).

Each call of rand will return A with the next of a 256 number long sequence of random numbers.

You can change the multiplier (5) and the addition (&45) to many different pairs and get different sequences.

One drawback of this method is that the lsb toggels each call - to avoid this, you could use a 16 bit number and take a selection of bits from the middle.

.rand
LDA seed
ASL A
ASL A
CLC
ADC seed
CLC
ADC #&45
STA seed
RTS

See the forums [1] for a BBC sample program, .ssd and discussion.