Page 1 of 2

Some interesting random thing

Posted: Fri Nov 16, 2012 11:31 am
by Ubermann
There may be some LUA programmers that could have a bit trouble getting true random numbers.
Google will tell you several methods, but I tested them and they may have different flaws, such as:

- You can't get two random numbers without waiting one second for the os.time() refreshes. This is a big flaw when you need many random nums in little time.
- The first random number in every sequence is always the same.


So I managed to get a simple yet powerful method to get true random numbers without the need to wait 1 second, and all of them different, even the first in every sequence.

The method uses Löve API and will not work without it.

It's composed by two methods. The first one is a milliseconds chronometer, the second one returns a random number between A and B, which are passed as parameters.

Code: Select all

-- set this variable as global visivility
miliseconds = nil

function chronometer()
    -- count miliseconds based on game time, usually a bit milliseconds per frame
    miliseconds = miliseconds + love.timer.getDelta()

    -- we don't know how much the game will last, so we better restart the counter to zero
    -- to avoid possible overflows
    if millisecond * 1000 >= 1 then milliseconds = 0 end
end


function randomNumber(A, B)
    -- generate a new random seed using our milliseconds chronometer
    -- you can omit os.time() here next. It's just to illustrate that you can use whatever method you want plus the milliseconds
    math.randomseed(os.time() + milliseconds)

   return math.random(A, B)
end

Next, include everything in your code, and call it like this: "print ( randomNumber(min, max) )"


The only drawback is if the game generates numbers faster than millisecond speeds, which I think could be easily avoided by generating numbers only once per frame.

Re: Some interesting random thing

Posted: Fri Nov 16, 2012 12:00 pm
by kikito
Executing randomseed every time before getting a random number is a Very Bad Idea.

Have you tried *not* doing it? Just use math.random:

Code: Select all

math.random(A,B)

Re: Some interesting random thing

Posted: Fri Nov 16, 2012 12:41 pm
by Ubermann
kikito wrote:Executing randomseed every time before getting a random number is a Very Bad Idea.

Have you tried *not* doing it? Just use math.random:

Code: Select all

math.random(A,B)
True.

Then those webpages that say in LUA you need to call os.randomseed() everytime are either obsolete or absolutely wrong.

Re: Some interesting random thing

Posted: Fri Nov 16, 2012 2:58 pm
by kikito
Probably yes. Which pages are those?

Re: Some interesting random thing

Posted: Fri Nov 16, 2012 4:05 pm
by Ubermann
https://www.google.es/search?q=lua+random+number

Gives some pages where math.random() is always being used with math.randomseed()

But I don't know why I didn't look to luausers webpage first. /facepalm

Re: Some interesting random thing

Posted: Sun Nov 18, 2012 7:49 pm
by SiENcE
Do this and everything is fine.

Code: Select all

	-- Initialize the pseudo random number generator
	-- The first random number you get is not really 'randomized' (at least in Windows 2K and OS X).
	-- To get better pseudo-random number just pop some random number before using them for real:
	math.randomseed(os.time())
	math.random(); math.random(); math.random()
Than you only need to use math.random(a,b).

But be carefull, do not use this for cryptographic operations. The entropy might differ from OS, Hardware and Luaversion!

Re: Some interesting random thing

Posted: Sun Nov 18, 2012 8:20 pm
by Dattorz
Actually, the default love.run() initializes the random seed automatically: love.run
The entropy might differ from OS, Hardware and Luaversion!
I'm pretty sure Lua just uses the C implementation of the random number generator, which differs based on, well, implementation. If you're generating random numbers for behaviors that you want to be 100% reproducible (EG. the classic Sonic games where bosses use the same infinite sequence of moves each time), this isn't a very workable solution because people on different operating systems are going to get different results. At some point I'm probably going to implement my own random number generator and, if possible, monkey patch Lua's random functions to use this generator instead.

Re: Some interesting random thing

Posted: Sun Nov 18, 2012 9:10 pm
by slime
Dattorz wrote:Actually, the default love.run() initializes the random seed automatically: love.run
The entropy might differ from OS, Hardware and Luaversion!
I'm pretty sure Lua just uses the C implementation of the random number generator, which differs based on, well, implementation. If you're generating random numbers for behaviors that you want to be 100% reproducible (EG. the classic Sonic games where bosses use the same infinite sequence of moves each time), this isn't a very workable solution because people on different operating systems are going to get different results. At some point I'm probably going to implement my own random number generator and, if possible, monkey patch Lua's random functions to use this generator instead.
Or use LÖVE with LuaJIT. :P
LuaJIT wrote:LuaJIT uses a Tausworthe PRNG with period 2^223 to implement math.random() and math.randomseed(). The quality of the PRNG results is much superior compared to the standard Lua implementation which uses the platform-specific ANSI rand().
The PRNG generates the same sequences from the same seeds on all platforms and makes use of all bits in the seed argument. math.random() without arguments generates 52 pseudo-random bits for every call. The result is uniformly distributed between 0.0 and 1.0. It's correctly scaled up and rounded for math.random(n [,m]) to preserve uniformity.

Re: Some interesting random thing

Posted: Sun Nov 18, 2012 9:16 pm
by Dattorz
But then players will get a different result purely based on which Lua implementation they use. The whole point is to remove outside factors from the equation and make it so that the sequence of random numbers is always the same for a given random seed regardless of the system the code is running on top of.

Re: Some interesting random thing

Posted: Sun Nov 18, 2012 9:25 pm
by slime
Dattorz wrote:But then players will get a different result purely based on which Lua implementation they use. The whole point is to remove outside factors from the equation and make it so that the sequence of random numbers is always the same for a given random seed regardless of the system the code is running on top of.
Not an issue if your game only uses LuaJIT. :P