I'm working on a roguelike engine using love2d, and I'm really hitting a wall with lua's math.random() function to get, you know, random numbers. The first thing I did was try os.time() as my randomseed, but that kind of falls apart when you are hitting it multiple times within one second ( using the same randomseed will give you the same 'random' number result).
Any thoughts?
- Phil
good seeding for math.random
Re: good seeding for math.random
You only need to set math.randomseed once, afterwards, math.random will set the seed to whatever was produced with math.random.philnelson wrote:I'm working on a roguelike engine using love2d, and I'm really hitting a wall with lua's math.random() function to get, you know, random numbers. The first thing I did was try os.time() as my randomseed, but that kind of falls apart when you are hitting it multiple times within one second ( using the same randomseed will give you the same 'random' number result).
Any thoughts?
- Phil
Re: good seeding for math.random
aye, if you use math.randomseed(os.time) more than once a second all your really doing is resetting the seed back to its inital value, thus math.random will start off with the same (cough) random value
call it once, and if you want to mix math.random up a tad bit more, before using it in your application, do something like
so its not as predictable, for any given second of any given day (if your really that concerned about it)
call it once, and if you want to mix math.random up a tad bit more, before using it in your application, do something like
Code: Select all
math.randomseed(os.time)
for i = 1, math.random(10) do
math.random
end
-
- Party member
- Posts: 215
- Joined: Sun Jan 18, 2009 8:03 pm
Re: good seeding for math.random
If you change the argument to math.randomseed by a little, then the first math.random also changes only by a little. The following number however will always seem random. So osgeld's suggestion works but one math.random is enough .osgeld wrote:aye, if you use math.randomseed(os.time) more than once a second all your really doing is resetting the seed back to its inital value, thus math.random will start off with the same (cough) random value
call it once, and if you want to mix math.random up a tad bit more, before using it in your application, do something like
so its not as predictable, for any given second of any given day (if your really that concerned about it)Code: Select all
math.randomseed(os.time) for i = 1, math.random(10) do math.random end
The only issue therefore if the same seed was passed to math.randomseed, i.e. if someone started two instances of the game on the very same second (e.g. by setting his PC's time). If you really want to get around this, you can add some computation at the beginning of your code and then call os.clock(). This returns the time the program has been running, including milliseconds. Multiply by 1000 and add to your seed.
To recap: initialize with math.randomseed(os.time()); math.random().
If I haven't written anything else, you may assume that my work is released under the LPC License - the LÖVE Community. See http://love2d.org/wiki/index.php?title=LPC_License.
Re: good seeding for math.random
I haven't examined the behavior in depth, but in my lander game and some other prototypes I've made, even just calling randomseed once in init can produce undesirable 'random' numbers.
I first noticed when I increased the size of my starfield and had loads of stars with the same speeds.
Presently, my fix has been to provide random X offsets in init, which appears to work just fine. It is only after the init routine is called that my random numbers get b0rked. In the case of the stars, they are less likely to hit the world extent at the same time (or within the same second) as other stars so I have good continuity, but if I give them all random speeds with X = 0, when they recycle, I get a lot of duplicate positions and speeds, far too many to be random.
Again, I'm only calling math.randomseed(os.time) at init and I still get strange results.
I first noticed when I increased the size of my starfield and had loads of stars with the same speeds.
Presently, my fix has been to provide random X offsets in init, which appears to work just fine. It is only after the init routine is called that my random numbers get b0rked. In the case of the stars, they are less likely to hit the world extent at the same time (or within the same second) as other stars so I have good continuity, but if I give them all random speeds with X = 0, when they recycle, I get a lot of duplicate positions and speeds, far too many to be random.
Again, I'm only calling math.randomseed(os.time) at init and I still get strange results.
Re: good seeding for math.random
yea, theres a bit of a argument on the lua forums about calling math.random more than once before using it, and i see both sides, and practice none
really (and i learned this very quickly a LOOOOOOONG time ago) your randomness is kinda subject to what your trying to randomize
for example..
i have a "dance floor" in secondlife, it chooses a random number from 1 to 4, it doesnt really matter if i used the built in sudo rgn, or came up with a 9 page homebrew solution, its 4 possibility and its not going to seem all that super random, using the same stuff in a deck of 52 cards on the other hand seems to be totally random, so its all in perspective
really (and i learned this very quickly a LOOOOOOONG time ago) your randomness is kinda subject to what your trying to randomize
for example..
i have a "dance floor" in secondlife, it chooses a random number from 1 to 4, it doesnt really matter if i used the built in sudo rgn, or came up with a 9 page homebrew solution, its 4 possibility and its not going to seem all that super random, using the same stuff in a deck of 52 cards on the other hand seems to be totally random, so its all in perspective
-
- Prole
- Posts: 48
- Joined: Sun Feb 01, 2009 3:32 am
Re: good seeding for math.random
"seeming" random .vs. "being" random seems to be at issue, here. Getting numbers that "seem more random" is definitely a desirable goal, but how to accomplish it?
Re: good seeding for math.random
So you're trying to tell us that random is...random?osgeld wrote:yea, theres a bit of a argument on the lua forums about calling math.random more than once before using it, and i see both sides, and practice none
really (and i learned this very quickly a LOOOOOOONG time ago) your randomness is kinda subject to what your trying to randomize
for example..
i have a "dance floor" in secondlife, it chooses a random number from 1 to 4, it doesnt really matter if i used the built in sudo rgn, or came up with a 9 page homebrew solution, its 4 possibility and its not going to seem all that super random, using the same stuff in a deck of 52 cards on the other hand seems to be totally random, so its all in perspective
Humans will see patterns in smaller random ranges, it's true. I like to go back to the Dilbert cartoon where he's introduced to their random number generator, just spouting off NINE NINE NINE NINE, etc.
You can produce the same number a dozen times with a call to math.random and it can still be random, but there's too much consistency to the current observable behavior to try to say that. The higher I set my max stars, the more stars I see in a nice row, moving at exactly the same speed.
Two things seem to effect it.
The time between calls, and whether or not it's called in init().
I think it warrants more research and will try to find time tomorrow to figure it out.
-
- Party member
- Posts: 215
- Joined: Sun Jan 18, 2009 8:03 pm
Re: good seeding for math.random
I understand the reason you may wish to make this distinction but that an object is random can mean one of two things: the object is stochastic or the object is unpredictable/chaotic. The latter category can be formally defined with information theory (but only makes sense in the limit without further assumption) and applies even for a deterministic system. For this reason, one need not distinguish between "seeming random" and being random, treating the former as a subjective claim about the latter.philnelson wrote:"seeming" random .vs. "being" random seems to be at issue, here. Getting numbers that "seem more random" is definitely a desirable goal, but how to accomplish it?
If I haven't written anything else, you may assume that my work is released under the LPC License - the LÖVE Community. See http://love2d.org/wiki/index.php?title=LPC_License.
-
- Party member
- Posts: 215
- Joined: Sun Jan 18, 2009 8:03 pm
Re: good seeding for math.random
Could you simplify this problem instance as much as possible and provide a demo? Are you sure that it is not due to some granularity of the velocities?Tabasco wrote:I haven't examined the behavior in depth, but in my lander game and some other prototypes I've made, even just calling randomseed once in init can produce undesirable 'random' numbers.
I first noticed when I increased the size of my starfield and had loads of stars with the same speeds.
Presently, my fix has been to provide random X offsets in init, which appears to work just fine. It is only after the init routine is called that my random numbers get b0rked. In the case of the stars, they are less likely to hit the world extent at the same time (or within the same second) as other stars so I have good continuity, but if I give them all random speeds with X = 0, when they recycle, I get a lot of duplicate positions and speeds, far too many to be random.
Again, I'm only calling math.randomseed(os.time) at init and I still get strange results.
If I haven't written anything else, you may assume that my work is released under the LPC License - the LÖVE Community. See http://love2d.org/wiki/index.php?title=LPC_License.
Who is online
Users browsing this forum: No registered users and 2 guests