Re: 32-bit random numbers

new topic     » goto parent     » topic index » view thread      » older message » newer message

I'll have another go:
function rand32(atom n)
	atom r
	if n <= #3FFFFFFF then return rand(n)
	else
		r = rand(#100) - 1
		for i = 1 to 3 do
			r = r * #100 + rand(#100) - 1
		end for
		return remainder(r, n) + 1
	end if
end function

What does it do? It composes r out of 4 bytes, each byte is a random number
between 0 and 255 ( = "rand(#100)-1" ).
This makes r a random integer between 0 and #FFFFFFFF. The distribution should
be as uniform as rand (each byte is independent from the other bytes).
The "remainder(r, n) + 1" produces a random integer between 1 and n.

--
tommy online: http://users.pandora.be/tommycarlier
Euphoria Message Board: http://uboard.proboards32.com

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu