Hey its me again. Now this time, how can i get a random number between 100 and 700?
Sorry for multiple posts, but i dont think the mouse and random numbers are related to eachother.
Random numbers
Random numbers
I can't come up with a good signature!
- nevon
- Commander of the Circuloids
- Posts: 938
- Joined: Thu Feb 14, 2008 8:25 pm
- Location: Stockholm, Sweden
- Contact:
Re: Random numbers
Use the math module.
More info here: http://lua-users.org/wiki/MathLibraryTutorial
Code: Select all
--First we set the seed.
math.setrandomseed(os.time)
--If you're using Windows, the first random numbers will be quite uniform.
--Fix that by just discarding a couple of random values
math.random(); math.random(); math.random();
--And now we generate a number between 100 and 700
math.random(100,700)
Re: Random numbers
What nevon said, except: use math.randomseed() and os.time() instead of os.time to set the seed:
Also:
Calling math.random() multiple times doesn't make the numbers "more random". What does "more random" mean anyway? Coding advice that suggests to do so stems from the old days(tm), where a random generator could be coded in a way that returned the seed as initial random value. If you had a program that uses os.time() (a number that increases each second) as seed, and start that program with one second delay in between, you'd see the first value increase by one for each program instance. You'd prevent that by calling rand() one time after setting the seed. Nowadays (and with programs that don't run one instance per second), this isn't the case anymore.
Other advice on how to use C's rand() is probably not applicable in Lua, as it already uses rand in the correct way.[/size]
Code: Select all
math.randomseed(os.time())
All numbers returned by math.random() will be distributed uniformly - that's actually a good thing. You can transform the uniform distribution to any other one (e.g. normal) though.nevon wrote:Code: Select all
--If you're using Windows, the first random numbers will be quite uniform. --Fix that by just discarding a couple of random values math.random(); math.random(); math.random();
Calling math.random() multiple times doesn't make the numbers "more random". What does "more random" mean anyway? Coding advice that suggests to do so stems from the old days(tm), where a random generator could be coded in a way that returned the seed as initial random value. If you had a program that uses os.time() (a number that increases each second) as seed, and start that program with one second delay in between, you'd see the first value increase by one for each program instance. You'd prevent that by calling rand() one time after setting the seed. Nowadays (and with programs that don't run one instance per second), this isn't the case anymore.
Other advice on how to use C's rand() is probably not applicable in Lua, as it already uses rand in the correct way.[/size]
- nevon
- Commander of the Circuloids
- Posts: 938
- Joined: Thu Feb 14, 2008 8:25 pm
- Location: Stockholm, Sweden
- Contact:
Re: Random numbers
Whoops!vrld wrote:What nevon said, except: use math.randomseed() and os.time() instead of os.time to set the seed:Code: Select all
math.randomseed(os.time())
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Random numbers
I don't think nevon meant "uniform". Thing is, people have complained about the first few values all being alike, and all have said that calling math.random() a few times fixes that.vrld wrote:Also:
All numbers returned by math.random() will be distributed uniformly - that's actually a good thing. You can transform the uniform distribution to any other one (e.g. normal) though.nevon wrote:Code: Select all
--If you're using Windows, the first random numbers will be quite uniform. --Fix that by just discarding a couple of random values math.random(); math.random(); math.random();
Calling math.random() multiple times doesn't make the numbers "more random". What does "more random" mean anyway? Coding advice that suggests to do so stems from the old days(tm), where a random generator could be coded in a way that returned the seed as initial random value. If you had a program that uses os.time() (a number that increases each second) as seed, and start that program with one second delay in between, you'd see the first value increase by one for each program instance. You'd prevent that by calling rand() one time after setting the seed. Nowadays (and with programs that don't run one instance per second), this isn't the case anymore.
Other advice on how to use C's rand() is probably not applicable in Lua, as it already uses rand in the correct way.[/size]
Help us help you: attach a .love.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Random numbers
Yeah, it's all very nice how you say modern RNGs don't have this problem anymore, well, they do.
Re: Random numbers
Reminds me of this:Robin wrote:Thing is, people have complained about the first few values all being alike, and all have said that calling math.random() a few times fixes that.
Yeah, it's all very nice how you say they do have this problem, well, they don't.bartbes wrote:Yeah, it's all very nice how you say modern RNGs don't have this problem anymore, well, they do.
But let's actually have a look at that before making such claims:
Code: Select all
math.randomseed(os.time()) for i = 1, 5 do print(math.random()) end
Code: Select all
Gentoo SUSE WinXP Win7
---------------- ------------------- ---------------- ----------------
0.34058737584417 0.13838563446812 0.7230750450148 0.73024689474166
0.84193615887404 0.47572395413915 0.51796014282662 0.13504440443129
0.87171956797676 0.7024961345375 0.24887844477676 0.5012054811243
0.48991742380425 0.0038691856916385 0.55269020661031 0.4261909848323
0.22074050839094 0.47359758311584 0.12158574175237 0.78545487838374
C99 gives a naive example implementation of rand():
Code: Select all
static unsigned long int next = 1;
int rand(void) // RAND_MAX assumed to be 32767
{
next = next * 1103515245 + 12345;
return (unsigned int)(next/65536) % 32768;
}
void srand(unsigned int seed)
{
next = seed;
}
Obligatory XKCD:
- sharpobject
- Prole
- Posts: 44
- Joined: Fri Mar 18, 2011 2:32 pm
- Location: California
Re: Random numbers
What kind of RNG does lua expose? Is it just a linear congruential generator?
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Random numbers
IIRC, Lua just uses the C function, so that would be whatever that is. (I think it depends on the platform.)sharpobject wrote:What kind of RNG does lua expose? Is it just a linear congruential generator?
Help us help you: attach a .love.
Who is online
Users browsing this forum: TobyAdam and 0 guests