Re: 32-bit random numbers

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

Hi Juergen,

----------
> From: Juergen Luethje <j.lue at gmx.de>
> To: EUforum at topica.com
> Subject: Re: 32-bit random numbers
> Sent: 7 jul 2004 y. 2:47

[snipped]

> > 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   ----    blink
> 
> It seems to me, that your *first* version 
>  was correct, wasn't it?

No,  rand_0_N(#FFFFFFFF) gives the numbers in [0 .. #FFFFFFFF] range,
1 + rand_0_N(#FFFFFFFF) gives the numbers in [0+1 .. #FFFFFFFF+1] range,
so, to get [1..#FFFFFFFF] range for ? procedure, we must
write the command:
 ? 1 + rand_0_N(#FFFFFFFF - 1).
These 0 and  #FFFFFFFF-1 will be very-very-very rare,
but the bounds of output distribution 
may be pure -- 0 + 1 = 1  and  #FFFFFFFF - 1 + 1 = #FFFFFFFF.

> 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>
{{{


N = 6 is not very good for this function,
for N = 6 we have our old good rand(6),
which gives EU integer output without these
atoms, floors, good or not so good rounds,
32-bit floats etc.

Functions like rand_0_N (without floor) may be
useful for any fractional or too big numbers,
when the pure bounds of needed distribution 
are not so important as for N = 6 case.

I think, the function:

constant k = 1 / 1073741823
function Rand_0_N(atom N)
   return N * k * (rand(1073741823)-1)
end function

may be more useful.

Then you can:

for i=1 to 1000 do
? floor(Rand_0_N(100000000000))
end for

to get the random integer atoms in floor's
range (I just do not know what is the biggest
EU integer atom after the floor function),
or get the random atoms in EU full range :

for i=1 to 1000 do
? Rand_0_N(1000000.9999999) --
end for

And very useful function:

constant k = 1 / 1073741823
function rand_0_1()
  return k * (rand(1073741823)-1)
end function

This one lets you get then any needed
distribution - from Simpson to normal
and then to sh-sh-mormal  blink

Say,

function norm()
 atom N
      N=0
    for i=1 to 12 do
      N += rand_0_1()
    end for
 return N - 6
end function --

will give the random numbers with very
good almost pure normal distribution.

Regards,
Igor Kachan
kinz at peterlink.ru

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

Search



Quick Links

User menu

Not signed in.

Misc Menu