Random numbers in Love

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.
Post Reply
User avatar
Buddy4point0
Prole
Posts: 35
Joined: Wed Jan 12, 2011 9:29 pm
Location: Virginia

Random numbers in Love

Post by Buddy4point0 »

To get a random number in Love, should I just use:

Code: Select all

math.random()
offset by the time on the computer or something, or does Love has it's own, better, function for getting a random number?
╚»ßuddy4point0 ■
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Random numbers in Love

Post by nevon »

First you set the seed:

Code: Select all

math.randomseed(os.time())
And then you just get random numbers as usual:

Code: Select all

math.random()
User avatar
Buddy4point0
Prole
Posts: 35
Joined: Wed Jan 12, 2011 9:29 pm
Location: Virginia

Re: Random numbers in Love

Post by Buddy4point0 »

Okay, thanks. Yeah, that's what I meant by offset it by the time. ^^
Something to add in future builds of Love would be a math module!
╚»ßuddy4point0 ■
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Random numbers in Love

Post by bartbes »

There's a fork by itsnotabigtruck which contains just that.
EMB
Citizen
Posts: 70
Joined: Sat Jan 08, 2011 8:49 pm

Re: Random numbers in Love

Post by EMB »

If you want a REALLY random seed (Or want to change the seed more than once a second) then use this:

Code: Select all

math.randomseed(os.time()*math.random())
Request Programs
If Linux were a beer, it would be shipped in open barrels so that anybody could piss in it before delivery
User avatar
Buddy4point0
Prole
Posts: 35
Joined: Wed Jan 12, 2011 9:29 pm
Location: Virginia

Re: Random numbers in Love

Post by Buddy4point0 »

EMB wrote:If you want a REALLY random seed (Or want to change the seed more than once a second) then use this:

Code: Select all

math.randomseed(os.time()*math.random())
Haha, I might just do that.
╚»ßuddy4point0 ■
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Random numbers in Love

Post by kikito »

EMB wrote:If you want a REALLY random seed (Or want to change the seed more than once a second) then use this:

Code: Select all

math.randomseed(os.time()*math.random())
What is the benefit of changing the seed every second (or less)? Will the random numbers you get with that method differ from the random numbers you get with nevon's? I mean, will they be "more random" somehow?
When I write def I mean function.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Random numbers in Love

Post by Taehl »

I've found that changing the random seed too often /reduces/ the randomness of the numbers overall. Set it once at the beginning of the program, then leave it alone. It's fine for anything short of hardcore cryptography (but why would you be doing that in Love, anyway?).
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Random numbers in Love

Post by vrld »

EMB wrote:

Code: Select all

math.randomseed(os.time()*math.random())
Since an unseeded math.random() will always return the same value, this will do nothing more than what math.randomseed(os.time()) already did.
Seeding more than once in a game is not necessary (and as Taehl mentioned, it can even be destructive), unless you want predictable "random" numbers, i.e. a the same chain of numbers after the math.randomseed().
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 4 guests