Changes

Programming:Random Number Generator

924 bytes added, 11:29, 23 December 2010
Added section on generating random numbers from BASIC, and reorganised and renamed other headings
== 8-bit Random Number Generator BASIC ==
The BASIC command <tt>RANDOMIZE TIME</tt> can be used to generate random numbers, using the value of the <tt>TIME</tt> variable as a seed. The <tt>TIME</tt> variable is incremented every 1/300th of a second, so it is highly unlikely that the same set of numbers will be generated each time the BASIC program is run. However, after about five or six numbers are obtained using the <tt>RND</tt> command, the randomness of the numbers decreases dramatically. The listing below demonstrates this problem: <pre>10 MODE 220 FOR a=1 TO 2330 RANDOMIZE TIME40 FOR b=1 TO 2550 PRINT USING("## ");INT(RND*100);60 NEXT b70 PRINT80 NEXT a</pre> This problem can be overcome by using the commands <tt>RANDOMIZE TIME:RANDOMIZE RND</tt> instead. This re-initialises the random number seed and greatly improves the randomness of the numbers that are obtained using the <tt>RND</tt> command. == Machine code == === 8-bit random number generator === This is a very simple generator. It's Its function is x[i + 1] = (5 * x[i] + 1) mod 256. The only advantage is the small size and it's its simplicity.
'''Input:''' ''none''
</pre>
=== 8-bit Random Number Generator - random number generator (lookup table based ) ===
To use this routine you need to define 1 Byte byte of RAM for the ''RandomPtr''.
'''Input:''' ''none''
add a,l
ld l,a
jr nc,.skip
inc h
.skip: ld a,(hl)
</pre>
=== 16-bit Random Number Generator random number generator ===
This algorithm uses a similar method, but returns much better results.
</pre>
=== 16-bit Random Number Generator random number generator using 'r' the R register ===
A pseudo random generator using the 'r' R register for the first byte of the random number. It then uses this number as a seed to feed the 8-bit random number generator to generate the other byte of the random value.
'''Input:''' ''none''
ret
</pre>
 
[[Category:Programming]]
883
edits