Some interesting random thing

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
Ubermann
Party member
Posts: 146
Joined: Mon Nov 05, 2012 4:00 pm

Some interesting random thing

Post 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.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Some interesting random thing

Post 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)
When I write def I mean function.
User avatar
Ubermann
Party member
Posts: 146
Joined: Mon Nov 05, 2012 4:00 pm

Re: Some interesting random thing

Post 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.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Some interesting random thing

Post by kikito »

Probably yes. Which pages are those?
When I write def I mean function.
User avatar
Ubermann
Party member
Posts: 146
Joined: Mon Nov 05, 2012 4:00 pm

Re: Some interesting random thing

Post 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
User avatar
SiENcE
Party member
Posts: 810
Joined: Thu Jul 24, 2008 2:25 pm
Location: Berlin/Germany
Contact:

Re: Some interesting random thing

Post 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!
User avatar
Dattorz
Citizen
Posts: 66
Joined: Sat Oct 27, 2012 12:32 am
Contact:

Re: Some interesting random thing

Post 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.
User avatar
slime
Solid Snayke
Posts: 3172
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Some interesting random thing

Post 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.
User avatar
Dattorz
Citizen
Posts: 66
Joined: Sat Oct 27, 2012 12:32 am
Contact:

Re: Some interesting random thing

Post 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.
User avatar
slime
Solid Snayke
Posts: 3172
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Some interesting random thing

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

Who is online

Users browsing this forum: No registered users and 12 guests