Page 1 of 5

Random number?

Posted: Mon Jun 24, 2013 10:04 am
by Teraku
I'm trying to make a small game, and I need some random integers. Like, a random integer between 1 and 6, for example. Or between 150 and 550.

How would I go about doing this?

Re: Random number?

Posted: Mon Jun 24, 2013 10:06 am
by Robin
You can use math.random(1, 6)

Re: Random number?

Posted: Mon Jun 24, 2013 10:26 am
by batatinha
If you call math.random too often you better call math.randomseed(os.time()) before of math.random...

Re: Random number?

Posted: Mon Jun 24, 2013 11:10 am
by Robin
batatinha wrote:If you call math.random too often you better call math.randomseed(os.time()) before of math.random...
No, don't do that. The default love.run already does that, and the random number generated only needs to be seeded once.

Re: Random number?

Posted: Mon Jun 24, 2013 1:49 pm
by raidho36
No, actually it's a good practice to randomize it time to time. If you sure that between two randomizations the amount of math.random calls is really random (that is does not depends on random function but rather on random events) you can use math.random as a random seed. I.e. you can call randomization every 10 seconds, while amount of random usage in between is really random since it would depend on user interaction and in-game events going on, and framerate isn't entirely stable which adds up to total randomness even if every frame is precisely the same.

Re: Random number?

Posted: Mon Jun 24, 2013 2:03 pm
by Automatik
raidho36 wrote:No, actually it's a good practice to randomize it time to time.
Why?
And it's just a game after all. It's not a case of security.
I think things like the new random generator in 0.9.0 (love.math.random), can give better results than resetting the random seed.

Re: Random number?

Posted: Mon Jun 24, 2013 2:20 pm
by Robin
raidho36 wrote:No, actually it's a good practice to randomize it time to time.
I've never heard anyone claim that before. I've seem people do it, but they all had no clue how RNGs work.

Re: Random number?

Posted: Mon Jun 24, 2013 5:06 pm
by raidho36
Now first off, this is not RNG, it's PRNG, a huge difference. It's not random in any way actually. The thing I suggested math.randomseed ( math.random ( ) ) is a simplified version of emulating true random number generator in software, which would not involve gathering non-random generated random bytes. The more sophisticated does involves such, like gathering user keyboard and mouse input and using it to generate new random numbers.

Re: Random number?

Posted: Mon Jun 24, 2013 5:25 pm
by Automatik
Now first off, this is not RNG, it's PRNG, a huge difference.
Yes, and? Most RNG are PRNG.
The thing I suggested math.randomseed ( math.random ( ) ) is a simplified version of emulating true random number generator in software
How? You don't give any explanation or source.
It don't change anything since the seed will is still based on time, just indirectly. It's still as possible to guess the results.
which would not involve gathering non-random generated random bytes. The more sophisticated does involves such, like gathering user keyboard and mouse input and using it to generate new random numbers.
Seriously, what's the point?
We are talking about a game. A game. It's not about security. It's not about encryption. It's just about having "a number". A player won't try to guess what the (P)RNG will give.

Re: Random number?

Posted: Mon Jun 24, 2013 5:41 pm
by Boolsheet
I'm pretty shitty with number theory, but I don't think you can increase the entropy of a random stream by seeding its own randomness to itself. Depending on the generator, it may also cause it no longer to be uniform. Most implementations of C's rand function are LCGs and have a uniform distribution.

math.randomseed ( math.random ( ) ) is the same as math.randomseed(0) by the way. I guess you meant something different. Anyway, you also can't be sure that this gives you enough control so that the generator doesn't need a few steps to "warm up" again.