Generate Random Numbers

General discussion about LÖVE, Lua, game development, puns, and unicorns.
ths532
Prole
Posts: 1
Joined: Wed Aug 10, 2016 7:46 am

Generate Random Numbers

Post 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,
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Generate Random Numbers

Post by s-ol »


s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
Tjakka5
Party member
Posts: 243
Joined: Thu Dec 26, 2013 12:17 pm

Re: Generate Random Numbers

Post by Tjakka5 »

You can also use love.math.random; it's seed is automatically set.
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Generate Random Numbers

Post 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
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
pgimeno
Party member
Posts: 3672
Joined: Sun Oct 18, 2015 2:58 pm

Re: Generate Random Numbers

Post 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.
User avatar
4aiman
Party member
Posts: 262
Joined: Sat Jan 16, 2016 10:30 am

Re: Generate Random Numbers

Post 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?)
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Generate Random Numbers

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
pgimeno
Party member
Posts: 3672
Joined: Sun Oct 18, 2015 2:58 pm

Re: Generate Random Numbers

Post 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/
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Generate Random Numbers

Post 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
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
4aiman
Party member
Posts: 262
Joined: Sat Jan 16, 2016 10:30 am

Re: Generate Random Numbers

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

Who is online

Users browsing this forum: Ahrefs [Bot] and 13 guests