RE: 32-bit random numbers
- Posted by "Ricardo M. Forno" <rforno at uyuyuy.com> Jul 07, 2004
- 521 views
The resulting distributions are good, but there is a problem. As rand(power(2, 30) -1) can generate only 2^30 - 1 different numbers, no matter what you do with these numbers, you will get possibly only 2^30 - 1 different numbers between 1 and 2^32, and so you will never get one of the remaining 2^32 - 2^30 numbers, or 3/4ths of the total possibilities. Regards. ----- Original Message ----- From: Juergen Luethje <j.lue at gmx.de> To: <EUforum at topica.com> Sent: Tuesday, July 06, 2004 7:47 PM Subject: Re: 32-bit random numbers > > > Hi Igor, you wrote: > > > Hi again Jurgen, > > > > Me wrote: > > <snip> > > >> Get one trick more: > >> > >> To get the 32 bit rnd flat distribution > >> you can do the following steps: > >> > >> a. Get rnd number in [0,1] range from the EU standard rand() > >> b. Get rnd number in needed range from [0,1] range. > >> > >> It may be done as: > >> > >> }}} <eucode> > >> atom k > >> k = 1 / 1073741823 -- the biggest EU integer > >> atom rnd_0_1 > >> > >> function rand_0_N(atom N) > >> rnd_0_1 = k * (rand(1073741823)-1) > >> return floor(N * rnd_0_1) > >> end function > >> > >> for i=1 to 1000 do > >> ? 1 + rand_0_N(#FFFFFFFF) -- this distribution > >> -- is in [1,#FFFFFFFF] range > >> end for > >> </eucode> {{{ > > Cool! Why didn't I think of that? > > > Yes, it seems to be: > > > > for i=1 to 1000 do > > ? 1 + rand_0_N(#FFFFFFFF - 1) > > -- this distribution > > -- is in [1,#FFFFFFFF] range > > end for ---- > > It seems to me, that your *first* version was correct, wasn't it? > For testing, I used N=6: > > }}} <eucode> > atom k > k = 1 / 1073741823 -- the biggest EU integer > atom rnd_0_1 > > function rand_0_N(atom N) > rnd_0_1 = k * (rand(1073741823)-1) > return floor(N * rnd_0_1) > end function > > atom N, x, min, max > > N = 6 > min = 1 + rand_0_N(N) > max = min > for i=1 to 1000 do > x = 1 + rand_0_N(N) -- this distribution is in [1,6] range > if x < min then > min = x > elsif x > max then > max = x > end if > end for > printf(1, "range [%d,%d]", {min, max}) > </eucode> {{{ > > Thanks, Igor! > > Regards, > Juergen > > > >