Math.Random making same output every time...

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.
Bannana97
Citizen
Posts: 77
Joined: Wed May 16, 2012 2:49 pm

Math.Random making same output every time...

Post 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?
Santos
Party member
Posts: 384
Joined: Sat Oct 22, 2011 7:37 am

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

Post 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)
Last edited by Santos on Sun Jul 15, 2012 6:00 pm, edited 1 time in total.
User avatar
Qcode
Party member
Posts: 170
Joined: Tue Jan 10, 2012 1:35 am

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

Post 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.
Bannana97
Citizen
Posts: 77
Joined: Wed May 16, 2012 2:49 pm

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

Post by Bannana97 »

It isn't a problem anymore, seeing it worked without calling the extra math.randoms.

Thanks you two! :)
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

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

Post by Jasoco »

I thought 0.8.0 did this automatically now?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Post 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
Help us help you: attach a .love.
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

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

Post 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
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Post by Robin »

Not really, no.
Help us help you: attach a .love.
AaronV
Prole
Posts: 16
Joined: Sun Jul 08, 2012 4:11 am

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

Post 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
Last edited by AaronV on Mon Jul 16, 2012 4:02 pm, edited 1 time in total.
What have I gotten myself into?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Post by Robin »

What? You know you should only call randomseed once, right?
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 3 guests