[Return to top] 68k, 32 bit, pseudo random number generator. By Lee Davison.
Back


This is the 68k version of the code to generate a maximal length, 32 bit, pseudo random number sequence. The loop is repeated five times to hide the nature of the generator, at least from human eyes. This code is taken from the RND(n) function in EhBASIC68k.

* 32 bit 68k version of PRNG. Prng32 must be in RAM and must be
* initialised to a non zero value. Taking the 5th next number is	
* slower but helps hide the shift nature of this generator.

NextPRNG
	MOVEQ	#4,d2		* do this 5 times
	MOVE.l	Prng32,d0	* get current 
Ninc0
	MOVEQ	#0,d1		* clear bit count
	ROR.l	#2,d0		* bit 31 -> carry
	BCC	Ninc1		* skip increment if =0

	ADDQ.b	#1,d1		* else increment bit count
Ninc1
	ROR.l	#3,d0		* bit 28 -> carry
	BCC	Ninc2		* skip increment if =0

	ADDQ.b	#1,d1		* else increment bit count
Ninc2
	ROL.l	#5,d0		* restore PRNG longword
	ROXR.b	#1,d1		* EOR bit into Xb
	ROXR.l	#1,d0		* shift bit to most significant
	DBF	d2,Ninc0	* loop 5 times

	MOVE.l	d0,Prng32	* save back to seed word
	RTS

Prng32
	ds.l	1		* random number store

The 6502 8 bit version can be found here and the Z80 8 bit version can be found here.


e-mail me [e-mail]
Last page update: 25th April, 2003.