Generate Random Numbers

General discussion about LÖVE, Lua, game development, puns, and unicorns.
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 »

...No. Let me try being more clearer than the most transparent of synthetic crystal lattices.

Fact 1. - By default, Löve sets the seed of its default PRNG to the value given by os.time. Since you can't control time, it will be ever so different, meaning the generator will always give back a totally different run of values, each startup.

Fact 2. - If you took the "offending" line out, or edited it so that the seed value was a constant (like 0 or 1 or 2.34 or whatever), you could get back the same run of values, even on different startups.

Don't do this, since you'll be left without one that can give you "reliable" "random" numbers.

If you would set the seed to be the same as pgimeno set it, it would give you back the same numbers he had it spit out. That's the point.

Procedurally Generated worlds, like in terraria; the universe, planets therein, their biomes, stuff like that, all PRNG-s that have a set seed.

Then again, if the question is, can you have both a "random" one, and one with a seed you set, then yes. You can have as many as you want, of both types.

[wiki]RandomGenerator[/wiki] objects are the key. You can even set one to have the same seed as the "default", with getting the seed and then setting the new PRNG to have the same seed. (For whatever reason).

You could, alternatively, always save the seed of the default PRNG, and then set it, effectively swapping out states you want to use, but if your device is not a literal potato, it can handle more than one PRNG-s.
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 »

Thanks, zorg :)

Yes, the question was "can you have both a "random" one, and one with a seed you set, then yes. You can have as many as you want, of both types."
What made me confused was the pgimeno's post.
It leaves the impression that no matter when he starts a love2d project it gives him all the save values across all platforms for a given version of love2d.
I just though *it can't be true* and that's why I asked whether the default seed was hardcoded.
Sorry for any confusion I've brought.


I know how PRNG work since I've been using those for a while for procedurally generate worlds.
It's a good thing love2d has means to get the seed. That'll come in handy. :)

Thanks everyone for all the trouble!
User avatar
pgimeno
Party member
Posts: 3672
Joined: Sun Oct 18, 2015 2:58 pm

Re: Generate Random Numbers

Post by pgimeno »

zorg wrote:I'm guessing you mean with setting the seed to one specific value, right? :3
No, I mean in the main body rather than in a callback. The love.math generator is not seeded with time until love.run. Until then, I don't know what it is initialized with, but I consistently get the same result every time.
____
4aiman wrote: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.
Actually between loading main.lua and calling love.load. It's love.run that initializes it, and love.run runs after main.lua has been executed and before love.load is executed. See the default [wiki]love.run[/wiki]:

Code: Select all

function love.run()

	if love.math then
		love.math.setRandomSeed(os.time())
	end
...
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?
You can, but there's a catch. love.run passes os.time() to [wiki]love.math.setRandomSeed[/wiki]() to initialize it, and there are two problems with that. First, love.math.setRandomSeed needs an integer, therefore it will round the argument if it's not an integer. Second, at least under Linux (not sure about the others), os.time() does not have sub-second precision.

As a result, if you try to seed the generator by passing only os.time() in the same second that love.run is run, you'll get the same sequence in the main body as in love.load. For example:

Code: Select all

love.math.setRandomSeed(os.time())
print(love.math.random())
print(love.math.random())

function love.load()
  print(love.math.random())
  print(love.math.random())
end
That will very likely print two values and then the same two values (unless the clock changes to the next second between running main.lua and executing love.load).

A simple solution is to subtract 1 from os.time() in the main body like this:

Code: Select all

love.math.setRandomSeed(os.time()-1)
Or simply doing love.timer.sleep(1) in the main body to ensure that the second changes, but that slows down the program's startup unnecessarily.

Alternatively, it's possible to seed with sub-second precision using [wiki]love.timer.getTime[/wiki]() and some arithmetic to get an integer, but that may be a bit overkill, and there's still a small risk of initializing to the same value if you're not careful.
Ideally it would be cool to know whether those values generated by uninitialized generator differ from build to build (commit to commit).
I don't think they have changed. I get the same result in 0.9.0 as in 0.9.1 and 0.9.2, and the same result in 0.10.0 as in 0.10.1. Probably something changed in the PRNG from 0.9 to 0.10. I don't remember having seen that in the release notes, but my memory isn't that good and I'm lazy to check now ;)
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 »

While i do believe we answered the original question and then some with more than what was asked, so my final remarks to this would be that i did leave out all the "love.run will seed the default löve prng after the body of main.lua was loaded & executed" bit, and pgimeno did cover it well, but i got to say, if i really needed to use it pre-love.run, then i'd either modify love.run to not set the seed, and do it manually before, in main.lua, or i'd use a prng object only. My point is i wouldn't experiment with what the above post detailed, it's just not worth it, and it's frankly pointless. (from a coding standpoint, from an "i'm bored so i'm experimenting" standpoint, it does have merit. :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 »

Sorry, I missed the comments on this page when writing my previous reply.

The problem of getting the same numbers every time comes up fairly frequently on these forums, and I seem to recall having seen people complaining that they still get the same numbers when switching to love.math.random. If the OP had followed the advice of simply replacing math.random with love.math.random, he would have found he'd still get the same results if he did so using the example program he posted, which doesn't use any events. That's what I intended to warn about. Then the question of what to do if you want different numbers when main.lua is run was raised, and due to my experience with the pitfalls of seeding via os.time(), I warned about the problems of using the naïve approach of seeding the same way that love.run uses. To be able to act on a problem you need to be aware that the problem exists in the first place. And yes, I concur that creating your own generator object and seeding it, and simply ignoring love.math.random, is probably the best solution. Replacing love.run just for that reason is overkill IMO.
ihave~=come2compete
Prole
Posts: 3
Joined: Sun Jun 30, 2024 7:43 pm

Re: Generate Random Numbers

Post by ihave~=come2compete »

Attachments
record_000001.gif
record_000001.gif (715.98 KiB) Viewed 1461 times
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests