Proper random number generation and radian/degree.

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
boypink
Prole
Posts: 10
Joined: Sat Nov 15, 2008 5:40 am
Location: Canada

Re: Proper random number generation and radian/degree.

Post 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?
User avatar
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.

Post 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 :|
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)
surtic
Citizen
Posts: 74
Joined: Sat Jul 12, 2008 12:18 am

Re: Proper random number generation and radian/degree.

Post 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...
User avatar
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.

Post 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
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)
surtic
Citizen
Posts: 74
Joined: Sat Jul 12, 2008 12:18 am

Re: Proper random number generation and radian/degree.

Post 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.
User avatar
subrime
Citizen
Posts: 76
Joined: Thu Nov 13, 2008 6:18 pm
Location: Australia

Re: Proper random number generation and radian/degree.

Post 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
surtic
Citizen
Posts: 74
Joined: Sat Jul 12, 2008 12:18 am

Re: Proper random number generation and radian/degree.

Post 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.
User avatar
Kaze
Party member
Posts: 189
Joined: Sat Jul 19, 2008 4:39 pm
Location: Dublin, Ireland

Re: Proper random number generation and radian/degree.

Post 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
u9_
Citizen
Posts: 54
Joined: Thu Oct 23, 2008 7:12 am

Re: Proper random number generation and radian/degree.

Post 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).
User avatar
subrime
Citizen
Posts: 76
Joined: Thu Nov 13, 2008 6:18 pm
Location: Australia

patience, grasshopper

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

Who is online

Users browsing this forum: Bing [Bot] and 2 guests