Page 1 of 2

Math.Random making same output every time...

Posted: Sun Jul 15, 2012 5:42 pm
by Bannana97
Hey peoples!
I am running this code:

Code: Select all

Game = {Version="0.3.6", SessionId=tostring(math.random(11111, 99999))}
Every time I call Game.SessionId it's "11222", even if I load another game window...

Am I doing something obviously wrong here?

Re: Math.Random making same output every time...

Posted: Sun Jul 15, 2012 5:50 pm
by Santos
math.random needs to seeded, which is often done using os.time. Also, the first couple of numbers from math.random seem to be not-so-random (I'm not sure why! :D)

Try adding this before using using math.random.

Code: Select all

math.randomseed(os.time())
math.random()
math.random()
(Fun fact: This is already done in the default love.run)

Re: Math.Random making same output every time...

Posted: Sun Jul 15, 2012 5:56 pm
by Qcode
In love.load I'd recommend calling math.randomseed(os.time()) This will base random numbers on the time that the system is running so it's a different number every time. That should thoroughly randomize it. I'm not sure if this is still a problem, but you used to have to call math.random() a couple times before it started properly randomizing it. Try the first method and if the ID is still 11222 then after math.randomseed(os.time()) put math.random() 3 or 4 times. So it would look like this.

Code: Select all

function love.load()
   --stuff
   math.randomseed(os.time())
   math.random()
   math.random()
   math.random()
   --stuff
   Game = {Version="0.3.6", SessionId=tostring(math.random(11111, 99999))}
   --stuff
end
EDIT: Santos you beat me to it :) ! Oh well posting this anyway.

Re: Math.Random making same output every time...

Posted: Sun Jul 15, 2012 6:00 pm
by Bannana97
It isn't a problem anymore, seeing it worked without calling the extra math.randoms.

Thanks you two! :)

Re: Math.Random making same output every time...

Posted: Sun Jul 15, 2012 6:41 pm
by Jasoco
I thought 0.8.0 did this automatically now?

Re: Math.Random making same output every time...

Posted: Sun Jul 15, 2012 7:19 pm
by Robin
It does.

Because it is in love.run() it only works for stuff in LÖVE callbacks, not outside of them, so this should work:

Code: Select all

function love.load()
   --stuff
   Game = {Version="0.3.6", SessionId=tostring(math.random(11111, 99999))}
   --stuff
end

Re: Math.Random making same output every time...

Posted: Sun Jul 15, 2012 8:48 pm
by coffee
I was calling this at load this since old version. Does something like this no longer is useful in 0.8?

Code: Select all

function random_reset()
		math.randomseed (os.time())
		local rnd = math.random(10)
		for i = 1,rnd do
			math.random()
		end
end

Re: Math.Random making same output every time...

Posted: Sun Jul 15, 2012 9:38 pm
by Robin
Not really, no.

Re: Math.Random making same output every time...

Posted: Mon Jul 16, 2012 6:11 am
by AaronV
THE FOLLOWING IS BAD ADVICE BUT KEPT FOR HISTORIC REASONS
I was trying to help... :cry:
Careful with using system time, it's entirely possible to get the same result twice in other languages.

Like if you wanted to pump out a ton of random numbers in once update step, you could easily get a lot of the same number in a row.

By the sounds of it, this is the case in lua.

picture this:

random(1) = 48295
random(1) = 48295
random(2) = 138899
random(1) = 48295

random(1:15:20:49) = 385883465
random(1:15:20:49) = 385883465
random(1:15:20:49) = 385883465
random(1:15:20:50) = 299598
random(1:15:20:50) = 299598
random(1:15:20:50) = 299598
random(1:15:20:50) = 299598
random(1:15:20:51) = 94996090
random(1:15:20:51) = 94996090

see what i'm getting at?

a solution i sometimes use is to add some counter to the time and increment it every time i call a random number.

random(1:15:20:49 + 0) = 385883465
random(1:15:20:49 + 1) = 299598
random(1:15:20:50 + 2) = 8189293

Re: Math.Random making same output every time...

Posted: Mon Jul 16, 2012 12:29 pm
by Robin
What? You know you should only call randomseed once, right?