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
Post
by Bannana97 » Sun Jul 15, 2012 5:42 pm
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
Post
by Santos » Sun Jul 15, 2012 5:50 pm
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!
)
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.
Qcode
Party member
Posts: 170 Joined: Tue Jan 10, 2012 1:35 am
Post
by Qcode » Sun Jul 15, 2012 5:56 pm
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
Post
by Bannana97 » Sun Jul 15, 2012 6:00 pm
It isn't a problem anymore, seeing it worked without calling the extra math.randoms.
Thanks you two!
Jasoco
Inner party member
Posts: 3726 Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:
Post
by Jasoco » Sun Jul 15, 2012 6:41 pm
I thought 0.8.0 did this automatically now?
Robin
The Omniscient
Posts: 6506 Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:
Post
by Robin » Sun Jul 15, 2012 7:19 pm
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
coffee
Party member
Posts: 1206 Joined: Wed Nov 02, 2011 9:07 pm
Post
by coffee » Sun Jul 15, 2012 8:48 pm
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
Robin
The Omniscient
Posts: 6506 Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:
Post
by Robin » Sun Jul 15, 2012 9:38 pm
Not really, no.
AaronV
Prole
Posts: 16 Joined: Sun Jul 08, 2012 4:11 am
Post
by AaronV » Mon Jul 16, 2012 6:11 am
THE FOLLOWING IS BAD ADVICE BUT KEPT FOR HISTORIC REASONS
I was trying to help...
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?
Robin
The Omniscient
Posts: 6506 Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:
Post
by Robin » Mon Jul 16, 2012 12:29 pm
What? You know you should only call randomseed once , right?
Users browsing this forum: Google [Bot] and 3 guests