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:
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})
Thanks, Igor!
Regards,
Juergen
|
Not Categorized, Please Help
|
|