Page 2 of 3

Re: Proper random number generation and radian/degree.

Posted: Sat Nov 22, 2008 6:04 am
by boypink
surtic wrote:Just a quick note - Lua's random number generator has problems with the first few numbers.
Im curious to know. What bad values?

Re: Proper random number generation and radian/degree.

Posted: Sat Nov 22, 2008 3:59 pm
by qubodup
boypink wrote:Im curious to know. What bad values?

Code: Select all

$ cat test.lua 
for i=1,10 do
        print(math.random())
end

$ lua test.lua 
0.84018771715471
0.39438292681909
0.78309922375861
0.79844003347607
0.91164735793678
0.19755136929338
0.33522275571489
0.7682295948119
0.27777471080319
0.5539699557954
I don't know either :|

Re: Proper random number generation and radian/degree.

Posted: Sat Nov 22, 2008 5:12 pm
by surtic

Code: Select all

c:\programming>lua
Lua 5.1.2  Copyright (C) 1994-2007 Lua.org, PUC-Rio
> math.randomseed(os.time())
> =math.random(0,100)
3
> =math.random(0,100)
54
> =math.random(0,100)
52
> =math.random(0,100)
100
> ^Z

c:\programming>lua
Lua 5.1.2  Copyright (C) 1994-2007 Lua.org, PUC-Rio
> math.randomseed(os.time())
> =math.random(0,100)
3
> =math.random(0,100)
16
> =math.random(0,100)
89
> =math.random(0,100)
87
> ^Z

c:\programming>lua
Lua 5.1.2  Copyright (C) 1994-2007 Lua.org, PUC-Rio
> math.randomseed(os.time())
> =math.random(0,100)
3
> =math.random(0,100)
11
> =math.random(0,100)
79
> =math.random(0,100)
47
> ^Z
Amazing how the random sequence always starts with 3...

Re: Proper random number generation and radian/degree.

Posted: Sat Nov 22, 2008 6:23 pm
by qubodup
surtic wrote:Amazing how the random sequence always starts with 3...
This does not happen on my machine. It (starting number) frequently changes

Re: Proper random number generation and radian/degree.

Posted: Sat Nov 22, 2008 7:46 pm
by surtic
I think the problem was originally reported on Win2K and OS X. I see it on WinXP, so I got into the habit of always "popping" the first few random numbers.

Re: Proper random number generation and radian/degree.

Posted: Sun Nov 23, 2008 2:25 am
by subrime
This isn't a problem with lua or any particular operating system, you just need to understand a little more about the math.random function.

Firstly, lua doesn't have a random number generator. It just gives access to the random number function in the standard c library. This is obvious when you compare lua:

Code: Select all

print(math.random())
0.84018771715471
and the output of the small c program:

Code: Select all

#include <stdio.h>
#include <stdlib.h>

int main(void) {
  printf("%.14f\n",((double)random())/RAND_MAX);
  return 0;
}
that also gives the value of 0.84018771715471

The sequence of numbers (from math.random) should be identical every time lua is started. It's meant to be this way. If you want unpredictable numbers you need to set the math.randomseed with the system time. eg:

Code: Select all

math.randomseed(os.time())   -- just do this once at the start of your code
for j=1,10 do
  print(math.random())            -- your sequence of numbers will be different every time
end

Re: Proper random number generation and radian/degree.

Posted: Sun Nov 23, 2008 12:28 pm
by surtic
In my example I set the random seed every time and still get the number 3 at the beginning of every sequence.
It could be a problem with the C function, but it's still a problem.

You can check my post again to see what I mean. I am calling math.randomseed(os.time()) every time.

Re: Proper random number generation and radian/degree.

Posted: Sun Nov 23, 2008 1:50 pm
by Kaze
surtic wrote:In my example I set the random seed every time and still get the number 3 at the beginning of every sequence.
It could be a problem with the C function, but it's still a problem.

You can check my post again to see what I mean. I am calling math.randomseed(os.time()) every time.
How fast did you do it?
os.time returns a timestamp in seconds, and apparently adding 1 doesn't make much of a difference to the random number generator.

Code: Select all

Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> =os.time()
1227448072
> math.randomseed(1227448072)
> =math.random()
0.49220252082888
> math.randomseed(1227448073)
> =math.random()
0.49229407635731
> math.randomseed(1227448074)
> =math.random()
0.49241615039521

Re: Proper random number generation and radian/degree.

Posted: Sun Nov 23, 2008 5:04 pm
by u9_
I too have read (I believe in Lua documentation) that the first value is indeed not random and should be popped! As surtic states, this is clearly a problem for (at least some users).

patience, grasshopper

Posted: Sun Nov 23, 2008 8:35 pm
by subrime
surtic, your example isn't representative of how things work in the rest of the world. As Kaze noted, the system time is returned in seconds and running a script repeatedly within this timeframe will use the same value as a seed giving not only the same first number, but the same entire sequence. Of course a real world use will not be prone to this - if you finish with love in less than a second you're doing it wrong.

If you still get the same sequence when you wait a few seconds between runs of the script, I would suggest looking to see if the output of os.time is working on your system.