[Return to top] Z80, 8 bit, pseudo random number generator. By Lee Davison.
Back


This is a short peice of code that generates a maximal length, 8 bit, pseudo random number sequence. It was originally used to switch off and on all the LED's in a 8x30 matrix in a good random looking order (the un-needed values being skipped).

It's shorter, but slower, than using a lookup table and a counter.


; returns pseudo random 8 bit number in A. Only affects A.
; (r_seed) is the byte from which the number is generated and MUST be
; initialised to a non zero value or this function will always return
; zero. Also r_seed must be in RAM, you can see why......

rand_8:
	LD	A,(r_seed)	; get seed
	AND	#B8h		; mask non feedback bits
	SCF			; set carry
	JP	PO,no_clr	; skip clear if odd
	CCF			; complement carry (clear it)
no_clr:
	LD	A,(r_seed)	; get seed back
	RLA			; rotate carry into byte
	LD	(r_seed),A	; save back for next prn
	RET			; done

r_seed:
	DB	1		; prng seed byte (must not be zero)

The 6502 8 bit version can be found here and the 32 bit 68k version can be found here.


e-mail me [e-mail]
Last page update: 16th November, 2002.