1. 32-bit random numbers

Hi all,

for Euphoria's function rand(x), x may only be from 1 to the largest
positive value of type integer (#3FFFFFFF).

I would like to have a function, that can take any unsigned 32-bit integer
-- i.e. in the interval [0,#FFFFFFFF] -- as argument. Any ideas?

Regards,
   Juergen

-- 
 /"\  ASCII ribbon campain  |  The difference between men and boys
 \ /  against HTML in       |  is the price of their toys.
  X   e-mail and news,      |
 / \  and unneeded MIME     |  http://home.arcor.de/luethje/prog/

new topic     » topic index » view message » categorize

2. Re: 32-bit random numbers

Juergen Luethje wrote:
> 
> Hi all,
> 
> for Euphoria's function rand(x), x may only be from 1 to the largest
> positive value of type integer (#3FFFFFFF).
> 
> I would like to have a function, that can take any unsigned 32-bit integer
> -- i.e. in the interval [0,#FFFFFFFF] -- as argument. Any ideas?

You could try something like this...

 object vOldSeed vOldSeed = #69F5C10D
 function rand32()
    vOldSeed = vOldSeed * date()
vOldSeed = vOldSeed[1] + vOldSeed[2] + vOldSeed[3] + vOldSeed[4] +
    vOldSeed[5] + vOldSeed[6]
    vOldSeed += time() * (time() + 17)
    vOldSeed = floor(remainder(vOldSeed * (vOldSeed - 3), #3FFFFFFF))
    set_rand(vOldSeed)
return and_bits(rand(#3FFFFFFF), #FFFF) * #10000 + and_bits(rand(#3FFFFFFF),
    #FFFF)
 end function

-- 
Derek Parnell
Melbourne, Australia

new topic     » goto parent     » topic index » view message » categorize

3. Re: 32-bit random numbers

Juergen Luethje wrote:
> 
> Hi all,
> 
> for Euphoria's function rand(x), x may only be from 1 to the largest
> positive value of type integer (#3FFFFFFF).
> 
> I would like to have a function, that can take any unsigned 32-bit integer
> -- i.e. in the interval [0,#FFFFFFFF] -- as argument. Any ideas?
> 

I believe the Mersenne Twister library in the archive generates 32-bit
integers...

new topic     » goto parent     » topic index » view message » categorize

4. Re: 32-bit random numbers

How to create a 32-bit random integer between 1 and n (n = 32-bit integer atom):

function rand32(atom n)
    if n <= #3FFFFFFF then return rand(n)
    else return rand(n - #3FFFFFFF) + rand(#3FFFFFFF) - 1
end function

PS: if you leave out the -1 at the end, the number 1 will never be chosen.

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

new topic     » goto parent     » topic index » view message » categorize

5. Re: 32-bit random numbers

Tommy Carlier wrote:
> How to create a 32-bit random integer between 1 and n (n = 32-bit integer
> atom):
> 
> function rand32(atom n)
>     if n <= #3FFFFFFF then return rand(n)
>     else return rand(n - #3FFFFFFF) + rand(#3FFFFFFF) - 1
> end function
> 
> PS: if you leave out the -1 at the end, the number 1 will never be chosen.

That will work, but keep in mind that the probability distribution
of the sum of two random numbers is not flat. For example,
if you roll two dice (1-6), the sum is far more likely to 
be 7 than 12. So what would I do? I'm not sure, but I guess you
could take two random 16-bit numbers and concatenate them into
a 32-bit number. I'd still have some slight worries though,
since the two 16-bit numbers would be consecutive numbers
from the same random number generator. Are they truly independent?
Of course not, but then rand() is really just giving you 
"pseudo-random" numbers anyway.

Regards,
   Rob Craig
   Rapid Deployment Software
   http://www.RapidEuphoria.com

new topic     » goto parent     » topic index » view message » categorize

6. Re: 32-bit random numbers

Tommy Carlier wrote:

> How to create a 32-bit random integer between 1 and n (n = 32-bit integer
> atom):
>
> function rand32(atom n)
>     if n <= #3FFFFFFF then return rand(n)
>     else return rand(n - #3FFFFFFF) + rand(#3FFFFFFF) - 1
      end if      -- smile
> end function
>
> PS: if you leave out the -1 at the end, the number 1 will never be chosen.

Hi, this function can not take a big 32-bit integer atom as argument.
If n > 2*#3FFFFFFF, then (n - #3FFFFFFF) > #3FFFFFFF, that means the
argument to the first call of rand() will be too big, and Euphoria
aborts with a syntax error.

Also, I want the rand32() function to behave like Euphoria's built-in
rand() function. That means, the returned values must have a uniform
distribution. Are you sure, that the sum
      rand(n - #3FFFFFFF) + rand(#3FFFFFFF)
is uniformly distributed?

Sorry, I don't have a statistical test for this at hand ATM.

Regards,
   Juergen

new topic     » goto parent     » topic index » view message » categorize

7. Re: 32-bit random numbers

Juergen Luethje wrote:
> Hi, this function can not take a big 32-bit integer atom as argument.
> If n > 2*#3FFFFFFF, then (n - #3FFFFFFF) > #3FFFFFFF, that means the
> argument to the first call of rand() will be too big, and Euphoria
> aborts with a syntax error.
> 
> Also, I want the rand32() function to behave like Euphoria's built-in
> rand() function. That means, the returned values must have a uniform
> distribution. Are you sure, that the sum
>       rand(n - #3FFFFFFF) + rand(#3FFFFFFF)
> is uniformly distributed?
> 
> Sorry, I don't have a statistical test for this at hand ATM.

You're totally right. I just typed in some function here, without really
thinking about it a lot. I just wanted to show how easy it would be to create a
function that generates 32-bit random numbers. The function I gave was just to
push others in the right direction.

As Robert already pointed out: the returned values are indeed not uniformly
distributed. Instead of adding 2 random integers, a different formula should be
used. I'm sure there are enough smart Euphorians to solve this problem.

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

new topic     » goto parent     » topic index » view message » categorize

8. Re: 32-bit random numbers

Isn't the Mersenne Twister library a proven and very good random number
generator (and already in the archive)?  The only problem is that it isn't as
fast as the built-in, but if you don't need a gazillion random numbers quickly it
isn't bad...

new topic     » goto parent     » topic index » view message » categorize

9. Re: 32-bit random numbers

Hello again,

I have to agree with Rob, that the probability distribution *might*
not be acceptable for some things, but i dont think Derek's 
solution was that far off.  I'd make a slight change to improve
it a little and i'd use if myself for everything except applications
that require encryption of data:

constant n=#10000

function Rand32()
  --no reason why you cant make this more inline...
  atom a,b,c

  a=rand(n)-1
  b=rand(n)-1
  c=a*n+b
  return c
end function


P.S. We havent heard from Wolf yet???

Take care,
Al

And, good luck with your Euphoria programming!

My bumper sticker: "I brake for LED's"


Robert Craig wrote:
> 
> Tommy Carlier wrote:
> > How to create a 32-bit random integer between 1 and n (n = 32-bit integer
> > atom):
> > 
> > function rand32(atom n)
> >     if n <= #3FFFFFFF then return rand(n)
> >     else return rand(n - #3FFFFFFF) + rand(#3FFFFFFF) - 1
> > end function
> > 
> > PS: if you leave out the -1 at the end, the number 1 will never be chosen.
> 
> That will work, but keep in mind that the probability distribution
> of the sum of two random numbers is not flat. For example,
> if you roll two dice (1-6), the sum is far more likely to 
> be 7 than 12. So what would I do? I'm not sure, but I guess you
> could take two random 16-bit numbers and concatenate them into
> a 32-bit number. I'd still have some slight worries though,
> since the two 16-bit numbers would be consecutive numbers
> from the same random number generator. Are they truly independent?
> Of course not, but then rand() is really just giving you 
> "pseudo-random" numbers anyway.
> 
> Regards,
>    Rob Craig
>    Rapid Deployment Software
>    <a href="http://www.RapidEuphoria.com">http://www.RapidEuphoria.com</a>
>

new topic     » goto parent     » topic index » view message » categorize

10. Re: 32-bit random numbers

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 message » categorize

11. Re: 32-bit random numbers

Andy Serpa wrote:

> Juergen Luethje wrote:
>>
>> Hi all,
>>
>> for Euphoria's function rand(x), x may only be from 1 to the largest
>> positive value of type integer (#3FFFFFFF).
>>
>> I would like to have a function, that can take any unsigned 32-bit integer
>> -- i.e. in the interval [0,#FFFFFFFF] -- as argument. Any ideas?
>
> I believe the Mersenne Twister library in the archive generates 32-bit
> integers...

Yes, it does. Thanks, Andy! The archive -- and this mailing list -- are
really "treasures of knowledge". smile

Regards,
   Juergen

new topic     » goto parent     » topic index » view message » categorize

12. Re: 32-bit random numbers

Derek Parnell wrote:

> Juergen Luethje wrote:
>>
>> Hi all,
>>
>> for Euphoria's function rand(x), x may only be from 1 to the largest
>> positive value of type integer (#3FFFFFFF).
>>
>> I would like to have a function, that can take any unsigned 32-bit integer
>> -- i.e. in the interval [0,#FFFFFFFF] -- as argument. Any ideas?
>
> You could try something like this...
>
>  object vOldSeed vOldSeed = #69F5C10D
>  function rand32()
>     vOldSeed = vOldSeed * date()
>     vOldSeed = vOldSeed[1] + vOldSeed[2] + vOldSeed[3] + vOldSeed[4] +
>     vOldSeed[5] + vOldSeed[6]
>     vOldSeed += time() * (time() + 17)
>     vOldSeed = floor(remainder(vOldSeed * (vOldSeed - 3), #3FFFFFFF))
>     set_rand(vOldSeed)
>     return and_bits(rand(#3FFFFFFF), #FFFF) * #10000 +
>     and_bits(rand(#3FFFFFFF), #FFFF)
>  end function

Very nice, thanks Derek!
Do you think there is a way, that the function can take an argument,
that denotes the maximum returned value, just like the 'n' in Euphoria's
rand(n) does?

Maybe the last line of your function could be replaced with something
like this:

while 1 do
ret = and_bits(rand(#3FFFFFFF), #FFFF) * #10000 + and_bits(rand(#3FFFFFFF),
   #FFFF)
   if ret <= n then return ret end if
end while


But such a loop could sometimes take some time ... getlost

Regards,
   Juergen

new topic     » goto parent     » topic index » view message » categorize

13. Re: 32-bit random numbers

Robert Craig wrote:

> Tommy Carlier wrote:
>> How to create a 32-bit random integer between 1 and n (n = 32-bit integer
>> atom):
>>
>> function rand32(atom n)
>>     if n <= #3FFFFFFF then return rand(n)
>>     else return rand(n - #3FFFFFFF) + rand(#3FFFFFFF) - 1
>> end function
>>
>> PS: if you leave out the -1 at the end, the number 1 will never be chosen.
>
> That will work,

Only for n <= 2*#3FFFFFFF, not for any 32-bit integer.

> but keep in mind that the probability distribution
> of the sum of two random numbers is not flat. For example,
> if you roll two dice (1-6), the sum is far more likely to
> be 7 than 12.

Yep. In the games that I know, where players have to roll two dice, they
can only get the most important points, when the sum is 2 or 12. When
the sum is 7, maybe nothing special happens, or sometimes rather nasty
things happen. smile

> So what would I do? I'm not sure, but I guess you
> could take two random 16-bit numbers and concatenate them into
> a 32-bit number.

That's what Derek's code does, too, if I understand it correctly.
If two highly skilled software-engineers say so, I think it's not a bad
idea. smile

> I'd still have some slight worries though,
> since the two 16-bit numbers would be consecutive numbers
> from the same random number generator. Are they truly independent?
> Of course not, but then rand() is really just giving you
> "pseudo-random" numbers anyway.

Thanks for your input, Rob!

Regards,
   Juergen

new topic     » goto parent     » topic index » view message » categorize

14. Re: 32-bit random numbers

Juergen Luethje wrote:
> 
> Derek Parnell wrote:
> 
> > Juergen Luethje wrote:
> >>
> >> Hi all,
> >>
> >> for Euphoria's function rand(x), x may only be from 1 to the largest
> >> positive value of type integer (#3FFFFFFF).
> >>
> >> I would like to have a function, that can take any unsigned 32-bit integer
> >> -- i.e. in the interval [0,#FFFFFFFF] -- as argument. Any ideas?
> >
> > You could try something like this...
> >
> >  object vOldSeed vOldSeed = #69F5C10D
> >  function rand32()
> >     vOldSeed = vOldSeed * date()
> >     vOldSeed = vOldSeed[1] + vOldSeed[2] + vOldSeed[3] + vOldSeed[4] +
> >     vOldSeed[5] + vOldSeed[6]
> >     vOldSeed += time() * (time() + 17)
> >     vOldSeed = floor(remainder(vOldSeed * (vOldSeed - 3), #3FFFFFFF))
> >     set_rand(vOldSeed)
> >     return and_bits(rand(#3FFFFFFF), #FFFF) * #10000 +
> >     and_bits(rand(#3FFFFFFF), #FFFF)
> >  end function
> 
> Very nice, thanks Derek!
> Do you think there is a way, that the function can take an argument,
> that denotes the maximum returned value, just like the 'n' in Euphoria's
> rand(n) does?
> 
> Maybe the last line of your function could be replaced with something
> like this:
> 
> <font color="#330033"></font>
> <font color="#0000FF">while </font><font color="#330033">1 </font><font
> color="#0000FF">do</font>
> <font color="#330033">   ret = </font><font
> color="#FF00FF">and_bits</font><font color="#330033">(</font><font
> color="#FF00FF">rand</font><font color="#993333">(</font><font
> color="#330033">#3FFFFFFF</font><font color="#993333">)</font><font
> color="#330033">, #FFFF) * #10000 + </font><font
> color="#FF00FF">and_bits</font><font color="#330033">(</font><font
> color="#FF00FF">rand</font><font color="#993333">(</font><font
> color="#330033">#3FFFFFFF</font><font color="#993333">)</font><font
> color="#330033">, #FFFF)</font>
> <font color="#0000FF">   if </font><font color="#330033">ret <= n </font><font
> color="#0000FF">then return </font><font color="#330033">ret </font><font
> color="#0000FF">end if</font>
> <font color="#0000FF">end while</font>
> <font color="#330033"></font>
> 
> But such a loop could sometimes take some time ... getlost
> 
> Regards,
>    Juergen

Maybe this will be useful...

-- rand32.e
-- Produces a pseudo-random integer between 1 and N

include machine.e
object vOldSeed vOldSeed = #69F5C10D

function rand32(atom N)
    integer a, b
    sequence d
    atom X

    d = date()
    d = vOldSeed * d
    vOldSeed = d[1] + d[2] + d[3] + d[4] + d[5] + d[6]
    vOldSeed += time() * (time() + 17)
    vOldSeed = floor(remainder(vOldSeed * (vOldSeed - 3), #3FFFFFFF))
    set_rand(vOldSeed)
    a = rand(#3FFFFFFF)
    vOldSeed += d[4] + d[5] + d[6]
    vOldSeed += (time() + 3.1427) * (time() + 619)
    vOldSeed = floor(remainder(vOldSeed * (vOldSeed - 3), #3FFFFFFF))
    set_rand(vOldSeed)
    b = rand(#3FFFFFFF)
    X =  and_bits(a, #FFFF) * #10000 + and_bits(b, #FFFF)
    return floor(X - floor(X/N)*N) + 1

end function
constant debug = find("DEBUG:RAND32", command_line())
if debug then
for i = 1 to 32 do
    printf(1, "%4d ", rand32(2))
end for
end if

-- 
Derek Parnell
Melbourne, Australia

new topic     » goto parent     » topic index » view message » categorize

15. Re: 32-bit random numbers

On Sat, 03 Jul 2004 20:44:00 +0200, Juergen Luethje <j.lue at gmx.de>
wrote:

>That's what Derek's code does, too, if I understand it correctly.
>If two highly skilled software-engineers say so, I think it's not a bad
>idea. smile

I was about to suggest the same, but was beaten to it blink
As I think was said, I would also have noted that #00010001,
#00020002, etc will be less likely., but then again rand() itself is
not perfect.

Pete

new topic     » goto parent     » topic index » view message » categorize

16. Re: 32-bit random numbers

It doesnt matter as you people wouldnt know a solution if
it jumped up and bit you on the ass smile


Take care,
Al

And, good luck with your Euphoria programming!

My bumper sticker: "I brake for LED's"

new topic     » goto parent     » topic index » view message » categorize

17. Re: 32-bit random numbers

Al Getz wrote:
> It doesnt matter as you people wouldnt know a solution if
> it jumped up and bit you on the ass smile
 
What on earth do you mean by this? It sounds like an insult but the "smiley"
is making it ambiguous.

-- 
Derek Parnell
Melbourne, Australia

new topic     » goto parent     » topic index » view message » categorize

18. Re: 32-bit random numbers

Andy Serpa wrote:

> Isn't the Mersenne Twister library a proven and very good random number
> generator (and already in the archive)?  The only problem is that it
> isn't as fast as the built-in, but if you don't need a gazillion random
> numbers quickly it isn't bad...

Yes, I got it from the archive, and it works nicely. Thanks for the hint.

Regards,
   Juergen

new topic     » goto parent     » topic index » view message » categorize

19. Re: 32-bit random numbers

Pete Lomax wrote:

> On Sat, 03 Jul 2004 20:44:00 +0200, Juergen Luethje <j.lue at gmx.de>
> wrote:
>
>> That's what Derek's code does, too, if I understand it correctly.
>> If two highly skilled software-engineers say so, I think it's not a bad
>> idea. smile
>
> I was about to suggest the same, but was beaten to it blink
> As I think was said, I would also have noted that #00010001,
> #00020002, etc will be less likely., but then again rand() itself is
> not perfect.

*Concatenation* of two uniformly distributed 16-bit values -- as Derek
and Rob suggested -- should IMHO result in a uniform distribution, too.

Rob said, that *addition* of two uniformly distributed values does
result in a distribution, that is not uniform.

Regards,
   Juergen

new topic     » goto parent     » topic index » view message » categorize

20. Re: 32-bit random numbers

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

new topic     » goto parent     » topic index » view message » categorize

21. Re: 32-bit random numbers

Derek Parnell wrote:

[snipped old text]

> Maybe this will be useful...
>
> -- rand32.e
> -- Produces a pseudo-random integer between 1 and N
>
> include machine.e
> object vOldSeed vOldSeed = #69F5C10D
>
> function rand32(atom N)
>     integer a, b
>     sequence d
>     atom X
>
>     d = date()
>     d = vOldSeed * d
>     vOldSeed = d[1] + d[2] + d[3] + d[4] + d[5] + d[6]
>     vOldSeed += time() * (time() + 17)
>     vOldSeed = floor(remainder(vOldSeed * (vOldSeed - 3), #3FFFFFFF))
>     set_rand(vOldSeed)
>     a = rand(#3FFFFFFF)
>     vOldSeed += d[4] + d[5] + d[6]
>     vOldSeed += (time() + 3.1427) * (time() + 619)
>     vOldSeed = floor(remainder(vOldSeed * (vOldSeed - 3), #3FFFFFFF))
>     set_rand(vOldSeed)
>     b = rand(#3FFFFFFF)
>     X =  and_bits(a, #FFFF) * #10000 + and_bits(b, #FFFF)
>     return floor(X - floor(X/N)*N) + 1
>
> end function
> constant debug = find("DEBUG:RAND32", command_line())
> if debug then
> for i = 1 to 32 do
>     printf(1, "%4d ", rand32(2))
> end for
> end if

Cool. Thanks, Derek!

Regards,
   Juergen

new topic     » goto parent     » topic index » view message » categorize

22. Re: 32-bit random numbers

Me wrote:

> Derek Parnell wrote:
>
> [snipped old text]
>
>> Maybe this will be useful...
>>
>> -- rand32.e
>> -- Produces a pseudo-random integer between 1 and N
>>
>> include machine.e
>> object vOldSeed vOldSeed = #69F5C10D
>>
>> function rand32(atom N)
>>     integer a, b
>>     sequence d
>>     atom X
>>
>>     d = date()
>>     d = vOldSeed * d
>>     vOldSeed = d[1] + d[2] + d[3] + d[4] + d[5] + d[6]
>>     vOldSeed += time() * (time() + 17)
>>     vOldSeed = floor(remainder(vOldSeed * (vOldSeed - 3), #3FFFFFFF))
>>     set_rand(vOldSeed)
>>     a = rand(#3FFFFFFF)
>>     vOldSeed += d[4] + d[5] + d[6]
>>     vOldSeed += (time() + 3.1427) * (time() + 619)
>>     vOldSeed = floor(remainder(vOldSeed * (vOldSeed - 3), #3FFFFFFF))
>>     set_rand(vOldSeed)
>>     b = rand(#3FFFFFFF)
>>     X =  and_bits(a, #FFFF) * #10000 + and_bits(b, #FFFF)
>>     return floor(X - floor(X/N)*N) + 1
>>
>> end function
>> constant debug = find("DEBUG:RAND32", command_line())
>> if debug then
>> for i = 1 to 32 do
>>     printf(1, "%4d ", rand32(2))
>> end for
>> end if
>
> Cool. Thanks, Derek!

I made a synthesis of yours and Tommy's code. smile

function rand32 (atom n)
   -- Produces a pseudo-random integer between 1 and n
   atom x
   integer hiWord, loWord

   if n <= #3FFFFFFF then
      return rand(n)
   else
      hiWord = rand(#10000) - 1
      loWord = rand(#10000) - 1
      x = hiWord*#10000 + loWord
      return remainder(x, floor(n)) + 1
   end if
end function


Derek, do you think using set_rand() is essential here?

Regards,
   Juergen

new topic     » goto parent     » topic index » view message » categorize

23. Re: 32-bit random numbers

Juergen Luethje wrote:
> return remainder(x, floor(n)) + 1

There might be a bug in here: you want a random number between 1 and n, so n
should also be a possible value.
If x is n, remainder(x, floor(n)) will return 0 instead of n: n can never be the
result of remainder(x, floor(n)).
I think you can solve this by changing it to: remainder(x + 1, floor(n)).

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

new topic     » goto parent     » topic index » view message » categorize

24. Re: 32-bit random numbers

Juergen Luethje wrote:
 [snip]
 
> Derek, do you think using set_rand() is essential here?

If you are after a pseudo-random generator then it would not be required,
but if you want truer randomness, then it would be helpful.
 
Each value returned by rand() is dependent on the previous value,
the initial seed and how many previous values you have gotten so far.

By inserting set_rand() with values not based on anything that rand()
returns, you will get more unpredictablity about which value will
be the next returned by rand(). Without this, you can 100% 'predict'
the value because it is calulated.

In general use, this is not a big issue, but in areas of gaming, encryption,
and other high-volume random numbers, it is important.

-- 
Derek Parnell
Melbourne, Australia

new topic     » goto parent     » topic index » view message » categorize

25. Re: 32-bit random numbers

Tommy Carlier wrote:
> 
> Juergen Luethje wrote:
> > return remainder(x, floor(n)) + 1
> 
> There might be a bug in here: you want a random number between 1 and n, so n
> should
> also be a possible value.
> If x is n, remainder(x, floor(n)) will return 0 instead of n: n can never be
> the result
> of remainder(x, floor(n)).
> I think you can solve this by changing it to: remainder(x + 1, floor(n)).

Hmmm...?

Doesn't remainder(X,N) return a value between 0 and N-1 inclusive? And if
we want a result that is between 1 and N, then we just add 1 to the result
of the remainder() call. Which is what Juergen did.

Using your suggestion, we still get a a value between 0 and floor(n)-1
inclusive. 

For example:
   remainder(100, 100) + 1 = 1 // Juergen
   remainder(100 + 1, 100) = 1 // Tommy

that is okay, but what about this...

   remainder(99, 100) + 1 = 100 // Juergen
   remainder(99 + 1, 100) = 0   // Tommy

-- 
Derek Parnell
Melbourne, Australia

new topic     » goto parent     » topic index » view message » categorize

26. Re: 32-bit random numbers

<plug type="shameless">
In the Win32Lib, there is a getRandInt() function which uses
a high degree of entropy to assist in getting close-to-random
values.
</plug>
-- 
Derek Parnell
Melbourne, Australia

new topic     » goto parent     » topic index » view message » categorize

27. Re: 32-bit random numbers

Derek Parnell wrote:

> Juergen Luethje wrote:
>  [snip]
>
>> Derek, do you think using set_rand() is essential here?
>
> If you are after a pseudo-random generator then it would not be required,
> but if you want truer randomness, then it would be helpful.
>
> Each value returned by rand() is dependent on the previous value,
> the initial seed and how many previous values you have gotten so far.
>
> By inserting set_rand() with values not based on anything that rand()
> returns, you will get more unpredictablity about which value will
> be the next returned by rand(). Without this, you can 100% 'predict'
> the value because it is calulated.
>
> In general use, this is not a big issue, but in areas of gaming, encryption,
> and other high-volume random numbers, it is important.

I see now. Thank you very much for the comprehensive explanation.

Regards,
   Juergen

new topic     » goto parent     » topic index » view message » categorize

28. Re: 32-bit random numbers

Derek Parnell wrote:

> <plug type="shameless">

*LOL*

> In the Win32Lib, there is a getRandInt() function which uses
> a high degree of entropy to assist in getting close-to-random
> values.
> </plug>

I looked at the source code of the function, and didn't understand
anything. However, I just can use it ... smile

So I have the Mersenne Twister from the archieves, getRandInt() from Win32Lib,
and the stuff discussed here. Very nice, thanks again!

Regards,
   Juergen

new topic     » goto parent     » topic index » view message » categorize

29. Re: 32-bit random numbers

Christian Cuvier wrote:

>> *Concatenation* of two uniformly distributed 16-bit values -- as Derek
>> and Rob suggested -- should IMHO result in a uniform distribution, too.
>>
>> Rob said, that *addition* of two uniformly distributed values does
>> result in a distribution, that is not uniform.
>>
>> Regards,
>>    Juergen
>
> Rob also said that, in theory, concatenation would be nice. The problem is
> concatenating adjacent random numbers, because the random generator is
> actually deterministic. For this reason, only 2^16 among all possible 2^32
> integers will be generated.

Maybe not all possible 2^32 integers will be generated, but why exactly
2^16? Also, when using the fancy code in combination with set_rand()
that Derek posted, no *adjacent* random numbers are concatenated.

My text that you quoted above, was a reply to Pete, when he wrote:
"As I think was said, I would also have noted that #00010001, #00020002,
etc will be less likely."
Sorry, I still don't see the reason why e.g. #00010001 would be less
likely than any other value, when concatenating 2 uniformly distributed
16-bit random numbers.

<snip>

Regards,
   Juergen

new topic     » goto parent     » topic index » view message » categorize

30. Re: 32-bit random numbers

Derek Parnell wrote:

> Tommy Carlier wrote:
>>
>> Juergen Luethje wrote:
>>> return remainder(x, floor(n)) + 1

Dear Tommy, don't confuse me, please. smile
I took that code from your post, dated Sat, 03 Jul 2004 18:51 UTC.
I only added the floor() function, but that is just for the case that
'n' is not an integer. Not important regarding the question here.

>> There might be a bug in here: you want a random number between 1 and n, so n
>> should
>> also be a possible value.
>> If x is n, remainder(x, floor(n)) will return 0 instead of n:

Yes, (because x is an integer) it will return 0. But not "instead of"
anything.

>> n can never be the result of remainder(x, floor(n)).

Right, but 'n-1' can be the result of remainder(x, floor(n)). And then 1
will be added.

>> I think you can solve this by changing it to: remainder(x + 1, floor(n)).

No. The result of that will always be in the range 0..n-1.
Even adding not 1, but 1000000 to x *before* calculating the remainder would
not change the range of the result. smile

> Hmmm...?
>
> Doesn't remainder(X,N) return a value between 0 and N-1 inclusive? And if
> we want a result that is between 1 and N, then we just add 1 to the result
> of the remainder() call. Which is what Juergen did.
>
> Using your suggestion, we still get a a value between 0 and floor(n)-1
> inclusive.
>
> For example:
>    remainder(100, 100) + 1 = 1 // Juergen
>    remainder(100 + 1, 100) = 1 // Tommy
>
> that is okay, but what about this...
>
>    remainder(99, 100) + 1 = 100 // Juergen
>    remainder(99 + 1, 100) = 0   // Tommy

I think you are right Derek, like Tommy was in the post where I picked
the regarding line from. smile

Regards,
   Juergen

new topic     » goto parent     » topic index » view message » categorize

31. Re: 32-bit random numbers

Hi Juergen,

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

[snipped] 

> So I have the Mersenne Twister from the archieves, 
> getRandInt() from Win32Lib,
> and the stuff discussed here. 
> Very nice, thanks again!

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:

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


Regards,
Igor Kachan
kinz at peterlink.ru

new topic     » goto parent     » topic index » view message » categorize

32. Re: 32-bit random numbers

On Mon, 05 Jul 2004 22:06:33 +0200, Juergen Luethje <j.lue at gmx.de>
wrote:

>My text that you quoted above, was a reply to Pete, when he wrote:
>"As I think was said, I would also have noted that #00010001, #00020002,
>etc will be less likely."
>Sorry, I still don't see the reason why e.g. #00010001 would be less
>likely than any other value, when concatenating 2 uniformly distributed
>16-bit random numbers.
I just wrote a quick test program (actually only a minute or so before
reading this post), over 10,000,000 iterations, which soundly proved
me *wrong* on that point. My bad.

Pete

new topic     » goto parent     » topic index » view message » categorize

33. Re: 32-bit random numbers

Sorry, I forgot to include shipping. $20 for shipping for a total of $70.

~Greg

new topic     » goto parent     » topic index » view message » categorize

34. Re: 32-bit random numbers

My e-mail client is soooo crashing. Time to go back to Outlook! Sorry
everyone!

new topic     » goto parent     » topic index » view message » categorize

35. Re: 32-bit random numbers

Hi again Jurgen,

Me wrote:

----------
> From: Igor Kachan <kinz at peterlink.ru>
> To: EUforum at topica.com
> Subject: Re: 32-bit random numbers
> Sent: 6 jul 2004 y. 2:15
> 
> Hi Juergen,
> 
> ----------
> > From: Juergen Luethje <j.lue at gmx.de>
> > To: EUforum at topica.com
> > Subject: Re: 32-bit random numbers
> > Sent: 6 jul 2004 y. 0:06
> 
> [snipped] 
> 
> > So I have the Mersenne Twister from the archieves, 
> > getRandInt() from Win32Lib,
> > and the stuff discussed here. 
> > Very nice, thanks again!
> 
> 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>
{{{


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

Regards,
Igor Kachan
kinz at peterlink.ru

new topic     » goto parent     » topic index » view message » categorize

36. 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? smile

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

new topic     » goto parent     » topic index » view message » categorize

37. Re: 32-bit random numbers

Hello again,


I hope i can respond to some posts before the list 'scrolls' out
of view.  That's going to be a problem sometimes.  I cant always get
back exactly the same (or next) day.

By the way, anyone know the period of the current Euphoria random
number generator?


Pete:
Maybe some people need more coffee smile

Derek:
Using 'cant' is an experiment of sorts, to find out if
the apostrophe is really needed for good communication.
No one has EVER said they couldnt understand what 'cant'
meant, over YEARS of emails and message boards.  True
this isnt the final word on 'cant' but it's good enough
for me smile
I'm fully aware about the distribution problems that can
come up, and i was the first one to mention this some
years ago here, which is why i suggested again

rnd=(rand(n)-1)*n+rand(n)-1

or the alternate form

rnd=(rand(n)-1)*n+rand(n-1)

depending on what range you need.

And what i meant by 'add' was of the form
a*n+b

not simply add two rand numbers.

Also, right after posting that proposed solution somebody else
posted the same formula he he.

BTW i didnt think yours was bad or anything, except for the rand(n) of
the first part, which of course doesnt work.


Igor:
My second language is Spanish.
As i was saying, when i said 'add' it was in the context where i
assumed a*n+b not simply a+b.  As you noted a*b doesnt work.

If you have flat distrib with rand(n), then you have flat distrib with
(rand(n)-1)*n+rand(n)-1 as well.  This is true because rand(n) is
assumed truely random.  If not, then going to 32 bits isnt going
to change it that much unless it's real bad to start with.
Yes, as someone else pointed out, in the prng sequence x(n+1)=f(x(n))
but f(x(n)) is probably remote enough from x(n) to be still considered
random, for all but the most demanding tasks, which brings us to the
second post, which mentioned a file in the archives.  In other words,
if you can get by with rand(n) then you can probably get by with the
additive (concatenated) form.
Lastly, when i said
"note you cant use rand(n) to generate 'a' above",
'a' is the first random part of the whole 32 bit number, and 'b' is the
second part.  You cant use a=rand(n) because Euphoria's
rand(n) function starts at one, not zero, so you would never get
a random 32 bit number less then hex #10000 (funny).

Juergen:
You'll have to clear up your point.  You're deduction sounds incorrect.
If you're talking about me, im lost without my spell checker smile

Christian:
I dont see how you get 2^16 possibility from two concatenated 2^16 random
sequences.  Is this what you meant?  Please explain how you arrived at
far less then 2^32 ...

All:

I'll post this again, so if anyone thinks it doesnt work, speak now...

n=#1000
r32=(rand(n)-1)*n+rand(n)-1

I could be wrong, so show me smile



Take care,
Al

And, good luck with your random number programming,
sounds like you're gonna need it smile

My bumper sticker: "I brake for LED's"

new topic     » goto parent     » topic index » view message » categorize

38. Re: 32-bit random numbers

Al Getz wrote:

> If you're talking about me,

In this thread, I was talking about 32-bit random numbers.

Regards,
   Juergen

new topic     » goto parent     » topic index » view message » categorize

39. Re: 32-bit random numbers

Hi Al,

[some things are snipped]

> rnd=(rand(n)-1)*n+rand(n)-1

Two rand() functions in your expression
give TWO random numbers each with flat
distribution, and expression gives the 
composition of TWO random numbers with
same flat distribution.

The distribution of this composition
is NOT the FLAT one.

Try to build a histogram of your rnd
number to see what a sort of distribution
you get.

Maybe it is good enough for some of your tasks,
I do not know.

> or the alternate form
> 
> rnd=(rand(n)-1)*n+rand(n-1)

This is not the alternate form of
the above case.

rand(n) and  rand(n-1) have different
flat distributions.

rand(n)     -- from 1 to n,   EU integers
rand(n-1)   -- from 1 to n-1, EU integers

So, think once more, Al, what do you want.

[some things are snipped]

Regards,
Igor Kachan
kinz at peterlink.ru

new topic     » goto parent     » topic index » view message » categorize

40. Re: 32-bit random numbers

Hello Ricardo,

You wrote:
----------
> From: Ricardo Forno <rforno at uyuyuy.com>
> To: EUforum at topica.com
> Subject: RE: 32-bit random numbers
> Sent: 7 jul 2004 y. 22:09
> 
> 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.

Yes, the distribution will be discrete, but this is
another question - what precision do you need in your
statistic task and what is precision of your measurements.

But to get the first histogram you need just 50 .. 60
random numbers and may use 10 .. 12 intervals.

To get the maximum number of different random integer
atoms in EU, in floor() range, without thinking about
the real distribution, this is the third question. 

Regards,
Igor Kachan
kinz at peterlink.ru


> 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? smile
> >
> > > 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?
> > 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

new topic     » goto parent     » topic index » view message » categorize

41. Re: 32-bit random numbers

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 message » categorize

42. Re: 32-bit random numbers

Ricardo Forno wrote:

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

I see. getlost
Thank you for this important information, Ricardo!

[snipped old text]

Regards,
   Juergen

new topic     » goto parent     » topic index » view message » categorize

43. Re: 32-bit random numbers

Hi Juergen,

You wrote:

----------
> From: Juergen Luethje <j.lue at gmx.de>
> To: EUforum at topica.com
> Subject: Re: 32-bit random numbers
> Sent: 9 jul 2004 y. 0:48
> 
> Ricardo Forno wrote:
> 
> > 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.
> 
> I see. getlost
> Thank you for this important information, Ricardo!
> 
> [snipped old text]


I see your  getlost  and can not sleep  blink
Dont  sad   be   none  or  better    smile

Try please the next one RNG:

constant K = 1073741823 -- max EU integer
function rand_atom(integer N)
    return K * (rand(N) - 1) + rand(K) - 1
end function


A short explanation.

The (rand(N) - 1) part gives the random number
of each next standard flat range. 
Say, if N = 1, then next output result will be 
rand(K) - 1,  i.e. from the normal EU range.
If N = 2, then output result will be:

a.    rand(K) - 1
 
or

b.    K + rand(K) - 1     

The probability of a or b cases is 1/2, so,
the output result will has the flat range 0 .. K-1
or K + 0 .. K + K - 1,  i.e.   0 .. K-1 .. K .. K + K - 1,
i.e.  0 .. K + K - 1  with discrete flat distribution,
which has step = 1  -- all possible integer numbers.

Well, if N = 3, then (rand(N) - 1) will be 2 or 1 or 0
with probability 1/3 of each case.

So, the output result will have 
0 .. K-1 .. K .. K+K-1 .. K+K .. K+K+K - 1 range
with flat distribution and step = 1

And so on.

It seems to be correct, no?

The question is - what maximum N will give
still integer atoms on output - not EU integer type,
but inner EU integer atoms.

Regards,
Igor Kachan
kinz at peterlink.ru

new topic     » goto parent     » topic index » view message » categorize

44. Re: 32-bit random numbers

Hi igor,

"K * (rand(N) - 1) + rand(K) - 1"

It looks flat, but what is your intended advantage over
N*(rand(N)-1)+rand(N)-1, {N=#1000}
??

Notice you can also extend that one:

N*(rand(N)-1)+rand(N)-1

goes to:

N*N*(rand(N)-1)+N*(rand(N)-1)+rand(N)-1

if N is an integer multiple of the number of terms minus 1,
or:

N*N*(rand(N2)-1)+N*(rand(N2)-1)+rand(N2)-1

if N2=number of terms minus 1 .

This gives the ability to get higher and higher random numbers smile



Take care,
Al

And, good luck with your Euphoria programming!

My bumper sticker: "I brake for LED's"

new topic     » goto parent     » topic index » view message » categorize

45. Re: 32-bit random numbers

Hi Al,

----------
> From: Al Getz <guest at RapidEuphoria.com>
> To: EUforum at topica.com
> Subject: Re: 32-bit random numbers
> Sent: 10 jul 2004 y. 21:10
> 
> posted by: Al Getz <Xaxo at aol.com>
> 
> 
> Hi igor,
> 
> "K * (rand(N) - 1) + rand(K) - 1"
> 
> It looks flat, but what is your intended advantage over
> N*(rand(N)-1)+rand(N)-1, {N=#1000}
> ??

Al, the proposed function is:

constant K = 1073741823 -- max EU integer
function rand_atom(integer N)
    return K * (rand(N) - 1) + rand(K) - 1
end function

Did you see in explanation that N << K ? 

This parameter (N) is the key
to understanding of flat character
of this distribution. We need the
maximum range of integer random
atoms with step = 1 in EU.
This is our task now.

And I am just trying to solve this
concrete task as far as I understand it.

Yes, formulas are similar, but they
are *different*, Al.

I can say nothing about the distribution
on your formula. I just see it is not flat.

But you do see just now that proposed new
function seems to give the flat distribution.

> Notice you can also extend that one:
> 
> N*(rand(N)-1)+rand(N)-1
> 
> goes to:
> 
> N*N*(rand(N)-1)+N*(rand(N)-1)+rand(N)-1
> 
> if N is an integer multiple of the number
> of terms minus 1,
> or:
> 
> N*N*(rand(N2)-1)+N*(rand(N2)-1)+rand(N2)-1
> 
> if N2=number of terms minus 1 .
> 
> This gives the ability to get higher 
> and higher random numbers smile

OK OK OK, Al, what a problem, use please your
functions as you want for the suited tasks,
and tell us what were the distributions.

> 
> Take care,
> Al

Regards,
Igor Kachan
kinz at peterlink.ru

new topic     » goto parent     » topic index » view message » categorize

46. Re: 32-bit random numbers

Igor Kachan wrote:


>  I can say nothing about the distribution
>  on your formula. I just see it is not flat.

Ok, it's one thing to *SAY* it's not flat,
for example, i can *SAY* YOURS isnt flat,
but it's another story to *PROVE* it's not flat.

You see now?

You can teach a 4 year old to say 'everything is not flat'
but it's not true, is it?

In other words, PROVE it's not flat.

The reason i say this is because it looks like the
distribution *IS* flat, but there is of course always
the chance i made a mistake in the analysis, im just human!
So, if you want me to understand why you say it is *NOT* flat,
please show me how you came to this conclusion.

BTW, i wasnt saying YOUR function isnt flat, im just asking
why you needed another function, that's all, and what the
benefits are.  I thought maybe you wanted higher and higher
random integers so i proposed another solution smile
The proposed solution goes up to ANY desired integer ceiling,
not just #FFFFFFFF or whatever.

Im not saying there is no chance i made a mistake, im just asking
that you show me why you say it's not flat, especially the

N*(rand(N)-1)+rand(N)-1, with N=#10000

formula?

BTW, by 'flat' i assumed you meant that there is equal chance
of getting any number from 0 to max, so there are no preferences.

BTW(2), it would also be good to have a formula to specify the
max, such as max=N*(N-1)+N-1
or something like that.


> But you do see just now that proposed new
> function seems to give the flat distribution.

Yes, but i was only asking what the reasoning behind
developing a new formula would be, if it does the same.
If it does something different, that's cool too smile


Take care,
Al

And, good luck with your Euphoria programming!

My bumper sticker: "I brake for LED's"

new topic     » goto parent     » topic index » view message » categorize

47. Re: 32-bit random numbers

Hi Al,

----------
> From: Al Getz <guest at RapidEuphoria.com>
> To: EUforum at topica.com
> Subject: Re: 32-bit random numbers
> Sent: 10 jul 2004 y. 23:36
> 
> 
> posted by: Al Getz <Xaxo at aol.com>
> 
> Igor Kachan wrote:
> 
> 
> >  I can say nothing about the distribution
> >  on your formula. I just see it is not flat.
> 
> Ok, it's one thing to *SAY* it's not flat,
> for example, i can *SAY* YOURS isnt flat,
> but it's another story to *PROVE* it's not flat.
> 
> You see now?

Yes, I see. It is not flat, your distribution.
This my affirmation is very common.
I can say nothing about concrete parameters of
your distribution, it is the same thing that
I can say nothing about the distribution on
your formula, yes.
 
Formula is yours, try to prove it is good.
This is not my problem, Al.

I am trying to explain that N must be <<< K
in proposed the rand_atom() function.

Did you understand my explanation?

> You can teach a 4 year old to say 
> 'everything is not flat' but it's not true,
> is it?

He he, Al, even an any 40 years old can not
understand the Einstein's theory about 
all curved spaces  blink

> In other words, PROVE it's not flat.

Al, distribution is yours, we need the flat one,
prove please yours one is flat.

BTW, it is the very complicated task 
to create a good flat RNG, very, ask Rob.

rand() is one of the good examples.
We use it for our tasks.

> The reason i say this is because it looks like the
> distribution *IS* flat, but there is of course always
> the chance i made a mistake in the analysis, im just 
> human!

I'm just human too, it seems to be, no?  blink

> So, if you want me to understand why you say 
> it is *NOT* flat, please show me how you came
> to this conclusion.

Al, it is very long another [OT] story, I worked
on Russian navy research many years, models-shmodels,
we used Monte Carlo method, these RNGs-sh-sh-mrngs ...
brrrr ...

Another story, Al, books-sh-m-books ...

Ricardo knows, he says too - not flat.

Believe him.
 
> BTW, i wasnt saying YOUR function isnt flat,
> im just asking why you needed another function,
> that's all, and what the benefits are.

We just need a flat discrete distribution in maximum
integer range with step = 1.
My function seems to give just this one.
No another needs.

> I thought maybe you wanted higher and higher
> random integers so i proposed another solution smile
> The proposed solution goes up to ANY desired integer
> ceiling, not just #FFFFFFFF or whatever.
> Im not saying there is no chance i made a mistake,
> im just asking that you show me why you say
> it's not flat, especially the
> 
> N*(rand(N)-1)+rand(N)-1, with N=#10000
> 
> formula?

Al, this formula gives the composition
of TWO random numbers, each one with SAME flat
distribution. N is not random, *each* call
of rand(N) dives unpredictable number from
the [1, N] range.

Read please some good book on this
subject to be sure. It is too long story
for this list and especially for my time.
 
> BTW, by 'flat' i assumed you meant that there
> is equal chance of getting any number from 0 to max,
> so there are no preferences.

Yes, it is flat as I understand it.
(Good question for that late stage of discussion.)
 
> BTW(2), it would also be good to have a formula
> to specify the max, such as max=N*(N-1)+N-1
> or something like that.

Maybe, I don't know.
 
> > But you do see just now that proposed new
> > function seems to give the flat distribution.
> 
> Yes, but i was only asking what the reasoning behind
> developing a new formula would be, if it does the same.

No, not the same. N and K. N << K. 
Then it seems to be flat. 
It requires the careful testing after discussion.

> If it does something different, that's cool too smile

Al, cool or not very cool it is not our question.
We need the flat distribution on full EU integer
atoms range with step = 1, for now.

> Take care,
> Al

Regards,
Igor Kachan
kinz at peterlink.ru

new topic     » goto parent     » topic index » view message » categorize

48. Re: 32-bit random numbers

Hi Igor, you wrote:

>> From: Juergen Luethje
>> Sent: 9 jul 2004 y. 0:48
>>
>> Ricardo Forno wrote:
>>
>>> 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.
>>
>> I see. getlost
>> Thank you for this important information, Ricardo!
>>
>> [snipped old text]
>
>
> I see your  getlost  and can not sleep  blink
> Dont  sad   be   none  or  better    smile

Don't worry! I'm sleeping very good, and I hope that you do so, too. smile

> Try please the next one RNG:
>
> }}}
<eucode>
>
> constant K = 1073741823 -- max EU integer
> function rand_atom(integer N)
>     return K * (rand(N) - 1) + rand(K) - 1
> end function
>
> </eucode>
{{{

>
> A short explanation.
>
> The (rand(N) - 1) part gives the random number
> of each next standard flat range.
> Say, if N = 1, then next output result will be
> rand(K) - 1,  i.e. from the normal EU range.
> If N = 2, then output result will be:
>
> a.    rand(K) - 1
>
> or
>
> b.    K + rand(K) - 1
>
> The probability of a or b cases is 1/2, so,
> the output result will has the flat range 0 .. K-1
> or K + 0 .. K + K - 1,  i.e.   0 .. K-1 .. K .. K + K - 1,
> i.e.  0 .. K + K - 1  with discrete flat distribution,
> which has step = 1  -- all possible integer numbers.
>
> Well, if N = 3, then (rand(N) - 1) will be 2 or 1 or 0
> with probability 1/3 of each case.
>
> So, the output result will have
> 0 .. K-1 .. K .. K+K-1 .. K+K .. K+K+K - 1 range
> with flat distribution and step = 1
>
> And so on.
>
> It seems to be correct, no?

I don't know. It has already been said, that it's not beneficial to
combine successive results of rand(). I'm afraid it's all a bit over my
head. Things concerning randomness are rather difficult IMHO. Anyway,
thanks Igor!

> The question is - what maximum N will give
> still integer atoms on output - not EU integer type,
> but inner EU integer atoms.
>
> Regards,
> Igor Kachan
> kinz at peterlink.ru

Regards,
   Juergen

new topic     » goto parent     » topic index » view message » categorize

49. Re: 32-bit random numbers

Hi Juergen,

[snipped old info]

> > I see your  getlost  and can not sleep  blink
> > Dont  sad   be   none  or  better    smile
> 
> Don't worry! I'm sleeping very good, 
> and I hope that you do so, too. smile

Thanks, I am glad and will be very good 
sleeping just now smile

[snipped new info]

> I don't know. It has already been said,
> that it's not beneficial to combine 
> successive results of rand(). 

There are cases and cases of those compositions in RNGs.

> I'm afraid it's all a bit over my
> head.

Do not be afraid, be euphoric  smile

> Things concerning randomness are rather 
> difficult IMHO. Anyway, thanks Igor!

I think, we'll have a not bad random numbers
generator for full EU integer atoms range.

> > The question is - what maximum N will give
> > still integer atoms on output - not EU integer type,
> > but inner EU integer atoms.

The question still stands, but it is not 
a too complicated quection, I think.

> Regards,
>    Juergen

Good Luck!

Regards,
Igor Kachan
kinz at peterlink.ru

new topic     » goto parent     » topic index » view message » categorize

50. Re: 32-bit random numbers

Hi Juergen,

This subject seems to be near the end,  for now  blink

Try please new function, it can give random 
integer atoms in full EU range with step=1.
Distribution is flat, as far as I can see.

include misc.e

type range(integer N)
    return N>=1 and N<=8388608
end type

constant K = 1073741823 -- the max EU integer
-- 1073741823 / 8388608 = 127.99999... -- i.e. N << K

global function rand_int_atom(range N)
       return K * (rand(N) - 1) + rand(K) - 1
end function
 
-- this function gives random integer atoms 
-- in [0.. up to 9007199246352383, by 1073741823] range 
-- with step = 1, flat distribution.


--- Testing -- flat or not flat

sequence RR

RR = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}

atom a01,a02,a03,a04,a05,a06,a07,a08,a09,a10,
     a11,a12,a13,a14,a15,a16,a17,a18,a19,a20
     
 atom SS
      SS = 9007199246352383 / 20
     a01 = 01 * SS
     a02 = 02 * SS
     a03 = 03 * SS
     a04 = 04 * SS
     a05 = 05 * SS
     a06 = 06 * SS
     a07 = 07 * SS
     a08 = 08 * SS
     a09 = 09 * SS
     a10 = 10 * SS
     a11 = 11 * SS
     a12 = 12 * SS
     a13 = 13 * SS
     a14 = 14 * SS
     a15 = 15 * SS
     a16 = 16 * SS
     a17 = 17 * SS
     a18 = 18 * SS
     a19 = 19 * SS
     a20 = 20 * SS
     
atom NN  
     
NN = 100000

atom Z
for i=1 to NN do
 Z=rand_int_atom(8388608)  --<-- max range 1073741823 * 8388608 - 1
    if Z>=  0 and Z< a01 then
  RR[01] += 1
 elsif Z>=a01 and Z< a02 then
  RR[02] += 1
 elsif Z>=a02 and Z< a03 then
  RR[03] += 1
 elsif Z>=a03 and Z< a04 then
  RR[04] += 1
 elsif Z>=a04 and Z< a05 then
  RR[05] += 1
 elsif Z>=a05 and Z< a06 then
  RR[06] += 1
 elsif Z>=a06 and Z< a07 then
  RR[07] += 1
 elsif Z>=a07 and Z< a08 then
  RR[08] += 1
 elsif Z>=a08 and Z< a09 then
  RR[09] += 1
 elsif Z>=a09 and Z< a10 then
  RR[10] += 1
 elsif Z>=a10 and Z< a11 then
  RR[11] += 1
 elsif Z>=a11 and Z< a12 then
  RR[12] += 1
 elsif Z>=a12 and Z< a13 then
  RR[13] += 1
 elsif Z>=a13 and Z< a14 then
  RR[14] += 1
 elsif Z>=a14 and Z< a15 then
  RR[15] += 1
 elsif Z>=a15 and Z< a16 then
  RR[16] += 1
 elsif Z>=a16 and Z< a17 then
  RR[17] += 1
 elsif Z>=a17 and Z< a18 then
  RR[18] += 1
 elsif Z>=a18 and Z< a19 then
  RR[19] += 1
 elsif Z>=a19 and Z< a20 then
  RR[20] += 1
 end if
end for

pretty_print(1, RR / NN, {0,1,1,1})

--- flat, probabilities are almost the same
--- in all intervals.

while get_key()=-1 do end while
 clear_screen()
 
pretty_print(1, RR, {0,1,1,1})

--- or number of cases in each interval is almost
--- the same.


To get the top bound of EU integer atoms  for
this task, I used the following program:

atom k, t
integer K, z, y

K = 1073741823

k=0
z=0
t=0
y=0

while 1 do
 z += 1
 k += K
  if  
    k + 1 = k  then  exit  --  Rob suggested this probe
  end if
 t += K
 y += 1
end while

printf(1, "%d\n", {k}) -- the first bad bound
printf(1, "%d\n", {z}) -- the number of first bad iteration
printf(1, "%d\n", {t}) -- the last good bound
printf(1, "%d\n", {y}) -- the number of last good iteration
printf(1, "%d\n", {t/y}) -- control
printf(1, "%d\n", {1073741823}) -- control


Regards,
Igor Kachan
kinz at peterlink.ru

new topic     » goto parent     » topic index » view message » categorize

51. Re: 32-bit random numbers

Hello again Igor,

Gee, im sorry but your number generator isnt 'flat'.
I have a friend that agrees -- NOT FLAT!

Fix it maybe, or read a good book?

Thanks anyway smile

Take care,
Al

And, good luck with your Euphoria programming!

My bumper sticker: "I brake for LED's"

new topic     » goto parent     » topic index » view message » categorize

52. Re: 32-bit random numbers

Hi Al,

----------
> From: Al Getz <guest at RapidEuphoria.com>
> To: EUforum at topica.com
> Subject: Re: 32-bit random numbers
> Sent: 13 jul 2004 y. 1:33
> 
> posted by: Al Getz <Xaxo at aol.com>
> 
> Hello again Igor,
> 
> Gee, im sorry but your number generator isnt 'flat'.
> I have a friend that agrees -- NOT FLAT!

OK, OK, OK, try yours one, in company  with
your friend, and tell me about the results  blink
 
> Fix it maybe, or read a good book?

I do not see, for now, what to fix,
my results are good enough for that task.

All spaces are curved, not flat, do you
remember?   blink

Tell me please what to fix, if it is not
yours and your friend's top secret.

What is a good book in English?

My favourite Russian book is
"Spravochnik po verojatnostnym paschetam",
M., Voenizdat, 1970, 536p.

> 
> Thanks anyway smile
>

Don't mention it anyway! No problem anyway!
 
> Take care,
> Al

Good Luck Anyway!   smile

Regards,
Igor Kachan
kinz at peterlink.ru

new topic     » goto parent     » topic index » view message » categorize

53. Re: 32-bit random numbers

Hello Igor,

Igor Kachan wrote:
> OK, OK, OK, try yours one, in company  with
> your friend, and tell me about the results  blink
>  
> I do not see, for now, what to fix,
> my results are good enough for that task.
> 
> Tell me please what to fix, if it is not
> yours and your friend's top secret.

It's not mine, it's yours, so you fix it.
It's too complicated for me to tell you here,
read a good book on the subject!

> 
> Good Luck Anyway!   smile
> 
> Regards,
> Igor Kachan


Take care, and good luck to you too,
Al

And, good luck with your Euphoria programming!

My bumper sticker: "I brake for LED's"

new topic     » goto parent     » topic index » view message » categorize

54. Re: 32-bit random numbers

Hello Ricardo,

You wrote:

>Date: Jul 13 5:29
>From: "Ricardo M. Forno" <rforno at uyuyuy.com> 
>Subject: RE: 32-bit random numbers 

>Hi, both Igor and Al.
>Actually I didn't say anything about these formulas. 
>In an older post, I was referring to another formula
>saying not that it wasn't flat, but that it didn't
>generate all possible numbers. It was flat, however.
>Regarding Al's formula and Igor's formula, it seems 
>to me they both produce flat distributions, but this
>is only shallow thinking and I haven't yet tried 
>to prove their flatness.
>Regards.

I'm sorry, Ricardo, yes, it was in Al's thread 
Re: Let's try this ONE more time smile

Your words are:
------
> 
> Hi, Al.
> 
> You said:
> 
> <snip>
> 
> > 1. For two added rnd numbers the distribution
> > should be the same.
> > It's multiplication that changes the distribution.
> 
> In fact, adding two or more random numbers 
> *changes* the distribution.
> Old FORTRAN routines used the sum of 12 random
> numbers (why exactly 12? Because this was 
> considered enough to get a good distribution)
> to get a Gaussian normal distribution.
> 
> Regards.

Yes, the simplest composition of 2 added random numbers
gives not flat, triangle, Simpson's distribution, 12 gives
almost already Gaussian not flat distribution.

So, I thought you are saying about *not flat* distributions
at all. But actually you are saying only about *changes*
in more common sense, yes?

Sorry again, my hurry.
 
Anyway, your words and your restraint shows 
your good understanding of the discussing question.

Thanks.

Regards,
Igor Kachan
kinz at peterlink.ru

new topic     » goto parent     » topic index » view message » categorize

55. Re: 32-bit random numbers

Igor wrote:

> Hi Juergen,
>
> This subject seems to be near the end,  for now  blink

Agreed. smile

> Try please new function, it can give random
> integer atoms in full EU range with step=1.
> Distribution is flat, as far as I can see.

<big snip>

Thanks, Igor!

Regards,
   Juergen

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu