Re: 32-bit random numbers
- Posted by "Juergen Luethje" <j.lue at gmx.de> Jul 04, 2004
- 594 views
Tommy Carlier wrote: > I'll have another go: > }}} <eucode> > 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 > </eucode> {{{ > 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. Yes, I think concatenation of four bytes works as well as concatenation of two 16-bit values. Thanks! Regards, Juergen