Page 1 of 2

Generate Random Numbers

Posted: Wed Aug 10, 2016 8:57 am
by ths532
Trying to generate different numbers each time.
But get the same numbers each time.
I would like to get different numbers each time

-- Generate random number between 1 and 49

Code: Select all

local y = 0

while ( y < 6 ) do

x = math.random( 1, 49 ) 
y = y + 1

print( x )

end
-- Same result each time:
-- 42 20 39 40 45

Thank You,

Re: Generate Random Numbers

Posted: Wed Aug 10, 2016 9:47 am
by s-ol

Re: Generate Random Numbers

Posted: Wed Aug 10, 2016 9:51 am
by Tjakka5
You can also use love.math.random; it's seed is automatically set.

Re: Generate Random Numbers

Posted: Wed Aug 10, 2016 10:08 am
by zorg
The whole reason is that math.randomseed is not called at the beginning of [wiki]love.run[/wiki], because Löve has a better, OS-agnostic PRNG function, that does have its seed set at the start up of the program.
See: [wiki]love.math.random[/wiki]
Edit: ninja'd :3

Re: Generate Random Numbers

Posted: Wed Aug 10, 2016 10:55 am
by pgimeno
Yes BUT, use love.math.random in one of the callbacks, like e.g. [wiki]love.load[/wiki], and not in the main body, because at the moment of running the main body it won't be initialized yet.

Example: if this is your main.lua:

Code: Select all

print(love.math.random())
it will always print the same number; however, if this is your main.lua:

Code: Select all

function love.load()
  print(love.math.random())
end
then it will generate a different number each run.

Re: Generate Random Numbers

Posted: Wed Aug 10, 2016 7:06 pm
by 4aiman
Wait, doesn't calling love.math.random twice let me use different numbers for the second and on calls? (Provided I'm doing that outside the love.load?)

Re: Generate Random Numbers

Posted: Wed Aug 10, 2016 11:14 pm
by zorg
Iirc that was only ever a dirty hack for the lua math.random call, because the generator used was so garbage, the first few values regardless of seed were always very similar.

A PRNG or pseudo-random number generator only guarantees these things: (or, it should, at least)
You have a function to set the seed, one to get it, and one to get back one value from the generator.
The PRNG somehow notes how many times it has been called for a value.
Using the same seed, you will get back the same values in the same order every time.

Of course, this can hinge on internals very much. If it uses float arithmetic for whatever reason, it might not be OS/Architecture/Whatever-compatible across vast computer configurations.

Re: Generate Random Numbers

Posted: Thu Aug 11, 2016 3:03 am
by pgimeno
4aiman wrote:Wait, doesn't calling love.math.random twice let me use different numbers for the second and on calls? (Provided I'm doing that outside the love.load?)
If I understand what you mean, the problem is that the sequence will always be the same. For me, this program:

Code: Select all

print(love.math.random())
print(love.math.random())
print(love.math.random())
print(love.math.random())
in LÖVE 0.10 always prints

Code: Select all

0.68980386685727
0.51625805945345
0.61529873214785
0.44257253117383
and in 0.9 it always prints

Code: Select all

0.47425898676362
0.16484757319101
0.18724158270136
0.89076602278798
That's not really random, in the sense that e.g. if you use these to generate enemies or the like, they will always be the same. It's a bit like this popular cartoon: https://xkcd.com/221/

Re: Generate Random Numbers

Posted: Thu Aug 11, 2016 6:12 am
by zorg
pgimeno wrote:If I understand what you mean, the problem is that the sequence will always be the same. For me, this program: (...) in LÖVE 0.10 always prints (...) and in 0.9 it always prints (...)
That's not really random, in the sense that e.g. if you use these to generate enemies or the like, they will always be the same.
I'm guessing you mean with setting the seed to one specific value, right? :3

Re: Generate Random Numbers

Posted: Thu Aug 11, 2016 7:15 am
by 4aiman
Judging by given explanations, the seed for PRNG is set to something hardcoded and only during the love.load it is being set to is something random.

Okay... I guess.

Now, can I set a seed for PRNG prior love.load to get some PR results which will differ from the ones posted by pgimeno?
Or that won't work at all?
Ideally it would be cool to know whether those values generated by uninitialized generator differ from build to build (commit to commit).

I can't really test that myself due to having no access to a love2d-enabled device and a *very* slow and costly Internet.

Can't you help me? :oops: