;NOTE: this is the statistically tested and true one, and an improvement 
;on the algorithm used to this day in Roguelike games, see Wikipedia.

;I do claim this to be an improved algorithm, but I say it while thinking I 
;know there's a way out of product linearity for PRNG from recent trials.

*** CalcRnd by Photon of Scoopex ***
*** Inspired by and improved from Park and Miller, 1988 ***
Seed:	dc.l 0
NextRnd:
	move.l Seed(PC),d0
	bsr.s CalcRnd
	move.l d0,Seed-R(A5)
	RTS
Rnd:	;d0=seed, returns new seed in d0 and d1. trashes d2-d3
	move.l 4-C(A6),d1
	rol.l d0,d1
	eor.l d1,d0
CalcRnd:		;d0=seed, returns new seed in d0 and d1. trashes d2-d3
	not.l d0	;make sure it doesnt get stuck on seed 0
	swap d0

	move.l d0,d1
	mulu #16807,d0

	swap d1
	mulu #16807,d1
	addx.l d0,d0	;vital for spatial progression

	move.l d0,d2
	clr.w d2
	swap d2

	move.l d1,d3
	add.l d2,d3	;overlap the partial sum
	and.l #$ffff,d0	;ext.l is ok, but does not traverse ALL 2^32 values.
	add.l d0,d1	;overlap the partial sum

	swap d3
	eor.l d3,d1	;combine (add is not 100% perfect for bitwise fidelity)
	move.l d1,d0	;result in d1 AND d0, new seed
	RTS
