Im curious to know. What bad values?surtic wrote:Just a quick note - Lua's random number generator has problems with the first few numbers.
Proper random number generation and radian/degree.
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Proper random number generation and radian/degree.
- qubodup
- Inner party member
- Posts: 775
- Joined: Sat Jun 21, 2008 9:21 pm
- Location: Berlin, Germany
- Contact:
Re: Proper random number generation and radian/degree.
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
lg.newImage("cat.png") -- made possible by lg = love.graphics
-- Don't force fullscreen (it frustrates those who want to try your game real quick) -- Develop for 1280x720 (so people can make HD videos)
-- Don't force fullscreen (it frustrates those who want to try your game real quick) -- Develop for 1280x720 (so people can make HD videos)
Re: Proper random number generation and radian/degree.
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
- qubodup
- Inner party member
- Posts: 775
- Joined: Sat Jun 21, 2008 9:21 pm
- Location: Berlin, Germany
- Contact:
Re: Proper random number generation and radian/degree.
This does not happen on my machine. It (starting number) frequently changessurtic wrote:Amazing how the random sequence always starts with 3...
lg.newImage("cat.png") -- made possible by lg = love.graphics
-- Don't force fullscreen (it frustrates those who want to try your game real quick) -- Develop for 1280x720 (so people can make HD videos)
-- Don't force fullscreen (it frustrates those who want to try your game real quick) -- Develop for 1280x720 (so people can make HD videos)
Re: Proper random number generation and radian/degree.
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.
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:
and the output of the small c program:
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:
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
Code: Select all
#include <stdio.h>
#include <stdlib.h>
int main(void) {
printf("%.14f\n",((double)random())/RAND_MAX);
return 0;
}
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.
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.
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.
How fast did you do it?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.
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.
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
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.
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.
Who is online
Users browsing this forum: Azzla, Bing [Bot] and 8 guests