Thread: Random numbers
View Single Post
Old 15 November 2020, 17:15   #3
Registered User
thomas's Avatar
Join Date: Jan 2002
Location: Germany
Posts: 6,183
I once looked at the algorithm used by C64 Basic. It's really simple: it takes the 32bit seed, EORs it by a large random number (probably a prime number) and orders the bytes backward.

The seed was then interpreted as a float and normalized to be between 0 and 1. But that's not necessary, you already get quite good results if you divide the seed by the desired range and use the remainder as result.

Something like this for random numbers between 1 and 6:

	move.l	seed,d0
	eor.l	#$87654321,d0
	swap.w	d0
	swap.l	d0
	swap.w	d0
	move.l	d0,seed
	divu	#6,d0
	swap.l	d0
	ext.l	d0
	addq.l	#1,d0
(I don't remember exactly, it might as well be ADD instead of EOR.)
thomas is offline  
Page generated in 0.04479 seconds with 11 queries