Random numbers

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
Anxiety
Prole
Posts: 49
Joined: Sat Apr 02, 2011 9:36 am
Location: Finland

Random numbers

Post by Anxiety » Sat Apr 02, 2011 10:58 am

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.
I can't come up with a good signature!

User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Random numbers

Post by nevon » Sat Apr 02, 2011 11:03 am

Use the math module.

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)
More info here: http://lua-users.org/wiki/MathLibraryTutorial

User avatar
Anxiety
Prole
Posts: 49
Joined: Sat Apr 02, 2011 9:36 am
Location: Finland

Re: Random numbers

Post by Anxiety » Sat Apr 02, 2011 11:21 am

Okay thanks.
I can't come up with a good signature!

User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Random numbers

Post by vrld » Sat Apr 02, 2011 11:46 am

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())
Also:
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();
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.
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]
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine

User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Random numbers

Post by nevon » Sat Apr 02, 2011 11:52 am

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())
Whoops! :oops:

User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Random numbers

Post by Robin » Sat Apr 02, 2011 12:44 pm

vrld wrote:Also:
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();
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.
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]
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.
Help us help you: attach a .love.

User avatar
bartbes
Sex machine
Posts: 4944
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Random numbers

Post by bartbes » Sat Apr 02, 2011 12:57 pm

Yeah, it's all very nice how you say modern RNGs don't have this problem anymore, well, they do.

User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Random numbers

Post by vrld » Sat Apr 02, 2011 2:53 pm

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.
Reminds me of this:
Image
bartbes wrote:Yeah, it's all very nice how you say modern RNGs don't have this problem anymore, well, they do.
Yeah, it's all very nice how you say they do have this problem, well, they don't.

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
But I'm sure the majority of all computers out there behave differently.

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; 
}
Even with that I don't get numbers that look too much alike (seed 1): 16838, 5758, 10113, 17515, 31051, 5627, 23010, 7419, 16212, 4086

Obligatory XKCD:
Image
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine

User avatar
sharpobject
Prole
Posts: 44
Joined: Fri Mar 18, 2011 2:32 pm
Location: California
Contact:

Re: Random numbers

Post by sharpobject » Sat Apr 02, 2011 3:00 pm

What kind of RNG does lua expose? Is it just a linear congruential generator?

User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Random numbers

Post by Robin » Sat Apr 02, 2011 3:08 pm

sharpobject wrote:What kind of RNG does lua expose? Is it just a linear congruential generator?
IIRC, Lua just uses the C function, so that would be whatever that is. (I think it depends on the platform.)
Help us help you: attach a .love.

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests