Last modified on 6 August 2006, at 14:11

Programming:Random Number Generator

Revision as of 14:11, 6 August 2006 by Octoate (Talk | contribs) (Initial page)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

8-bit Random Number Generator

This is a very simple generator. It's function is x[i + 1] = (5 * x[i] + 1) mod 256. The only advantage is the small size and it's simplicity.

Input: none

Output: A = a pseudo random number, period 256

Rand8	ld	a,Seed		; Seed is usually 0
	ld	b,a
	add	a,a
	add	a,a
	add	a,b
	inc	a		; another possibility is ADD A,7
	ld	(Rand8+1),a
	ret

16-bit Random Number Generator

This algorithm uses a similar method, but returns much better results.

Input: none

Output: A = a pseudo random number, period 65536

Rand16	ld	de,Seed		; Seed is usually 0
	ld	a,d
	ld	h,e
	ld	l,253
	or	a
	sbc	hl,de
	sbc	a,0
	sbc	hl,de
	ld	d,0
	sbc	a,d
	ld	e,a
	sbc	hl,de
	jr	nc,Rand
	inc	hl
Rand	ld	(Rand16+1),hl
	ret