Random number?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Teraku
Prole
Posts: 27
Joined: Mon Jun 24, 2013 10:01 am
Location: The Netherlands

Random number?

Post 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?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Random number?

Post by Robin »

You can use math.random(1, 6)
Help us help you: attach a .love.
batatinha
Prole
Posts: 22
Joined: Mon Jun 24, 2013 3:49 am

Re: Random number?

Post by batatinha »

If you call math.random too often you better call math.randomseed(os.time()) before of math.random...
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Random number?

Post 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.
Help us help you: attach a .love.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Random number?

Post 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.
Automatik
Citizen
Posts: 57
Joined: Sun Feb 17, 2013 7:05 pm

Re: Random number?

Post 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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Random number?

Post 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.
Help us help you: attach a .love.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Random number?

Post 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.
Automatik
Citizen
Posts: 57
Joined: Sun Feb 17, 2013 7:05 pm

Re: Random number?

Post 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.
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Random number?

Post 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.
Shallow indentations.
Locked

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 14 guests