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

Re: Proper random number generation and radian/degree.

Post by surtic »

Subrime, as I showed in my output a few message back, the first number is the same but the rest of the sequence isn't. I think Kaze showed that the reason is that "similar" seeds give a "similar" first number in the sequence (but the rest of the sequence is different).

Just to prove a point, here's the code:

Code: Select all

local t = os.time()
print(t)

math.randomseed(t)
print(math.random(0,99),
      math.random(0,99),
      math.random(0,99),
      math.random(0,99))
and here's the output:

Code: Select all

c:\programming>lua random.lua
1227481969
87      38      90      76

c:\programming>lua random.lua
1227481972
87      36      54      96

c:\programming>lua random.lua
1227481974
87      2       63      43

c:\programming>lua random.lua
1227481977
87      0       26      63

c:\programming>lua random.lua
1227481980
87      99      90      83

c:\programming>lua random.lua
1227481983
87      97      53      4

c:\programming>lua random.lua
1227481985
87      63      62      51

c:\programming>lua random.lua
1227481988
87      61      26      71

c:\programming>lua random.lua
1227481991
87      60      89      91

c:\programming>lua random.lua
1227481993
87      25      98      38

c:\programming>lua random.lua
1227481996
87      24      62      58

c:\programming>lua random.lua
1227481998
87      89      71      5

c:\programming>lua random.lua
1227482001
87      88      35      26

c:\programming>lua random.lua
1227482004
87      86      98      46

c:\programming>lua random.lua
1227482006
87      52      7       93

c:\programming>lua random.lua
1227482009
87      50      71      13

c:\programming>lua random.lua
1227482012
87      48      34      33

c:\programming>lua random.lua
1227482015
87      47      98      54

c:\programming>lua random.lua
1227482018
87      45      61      74

c:\programming>lua random.lua
1227482021
87      44      25      94

c:\programming>lua random.lua
1227482024
87      42      89      15

c:\programming>lua random.lua
1227482027
87      40      52      35

c:\programming>lua random.lua
1227482029
87      6       61      82

c:\programming>lua random.lua
1227482032
87      4       25      2

c:\programming>lua random.lua
1227482035
87      3       88      22

c:\programming>lua random.lua
1227482038
87      1       52      43

c:\programming>lua random.lua
1227482041
87      0       15      63

c:\programming>lua random.lua
1227482044
87      98      79      83

c:\programming>lua random.lua
1227482047
87      97      42      4

c:\programming>lua random.lua
1227482051
87      28      60      97

c:\programming>lua random.lua
1227482054
87      26      24      18

c:\programming>lua random.lua
1227482057
87      25      88      38

c:\programming>lua random.lua
1227482060
87      23      51      58

c:\programming>lua random.lua
1227482063
87      21      15      79
You'll notice that even the second number is not very random. Of course, other systems may vary, but I'll stick to throwing the first three random numbers away.
User avatar
subrime
Citizen
Posts: 76
Joined: Thu Nov 13, 2008 6:18 pm
Location: Australia

random superstitions

Post by subrime »

Kaze is most assuredly right, but you seem to be missing the reason why.

A program written using love/lua/c random function, initialised with the system time, is not going to be repeatedly run even every few seconds. Once your game has finished, you've logged off, gone to school or work or bed, and so on, the next time you start your game the os.time will be far removed from before, and the behaviour you see running a tiny script every few seconds will not happen.
surtic
Citizen
Posts: 74
Joined: Sat Jul 12, 2008 12:18 am

Re: Proper random number generation and radian/degree.

Post by surtic »

Last time it hit me was when I was writing a tetris clone. While debugging, I kept getting the same first block and it really annoyed me. It's true that if you run the program only occasionally (and not too many times in a row) then it doesn't matter - to be honest I've only played it once or twice since then, because I hate tetris... But when I was testing it it was irritating.

You will notice that I got the same first number for more than 100 seconds - it's very likely that you'll run your program more than once during this time when you are developing it. In my opinion it's not very professional for a game to have this problem anyway, especially when the solution is as simple as throwing the first few numbers away (or using a better random function).

Of course, if all of this doesn't convince you, feel free to use the first number... who am I to tell you what to do? ;)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 88 guests