PDA

View Full Version : Generate a random number with Lua



Jlee
November 12th, 2005, 12:28 AM
I looked through the on-line manual but couldn't find it. So can it be done?

Jlee
November 12th, 2005, 12:46 AM
OK found it:

r = math.random(12)
Returns a random number between 1 & 12.

Jlee
November 12th, 2005, 02:15 AM
The strange thing is that NR always returns '1' the first time it's executed. So as a workaround I just entered the above line of code twice in succession in my function.

Jlee
November 12th, 2005, 08:37 AM
mmm

After closer inspection it seems that when you close and Open NR Lua always returns the exact same sequence of numbers. So they are not random after all.

Does anyone know how to create a truly random number using Lua?

Ben S
November 12th, 2005, 09:18 AM
Is this on PPC, Windows, or both?

Rob H
November 12th, 2005, 09:46 AM
mmm

After closer inspection it seems that when you close and Open NR Lua always returns the exact same sequence of numbers. So they are not random after all.

Does anyone know how to create a truly random number using Lua?
You need to seed the RNG - Programming in Lua recommends
math.randomseed(os.date("%d%H%M%S"))

Jlee
November 12th, 2005, 09:47 AM
both

Jlee
November 12th, 2005, 09:49 AM
Thanks Rob.

So do I just put that code in my function before the match.random bit?

Rob H
November 12th, 2005, 09:51 AM
You only want it to be called the once - so I'd probably put it in OnCCFLoad()