math.random not working

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
MicroMacro
Citizen
Posts: 92
Joined: Fri May 30, 2014 2:30 am
Location: Boston, MA, USA
Contact:

math.random not working

Post by MicroMacro »

Hey there.
I'm trying to make the main menu select randomly between 4 love audio sources that I've created.

Problem is math.random won't work; it always returns 1.

The code couldn't be simpler:

Code: Select all

song = music.mood.happy[math.random(1,4)]
Anyone have any idea why math.random is only returning 1? I've tried putting it to a variable (i.e; music.mood.happy[randomNumVar]) but that also only returns 1.

Thanks.
https://github.com/ebernerd- where you can find all my work.
User avatar
MicroMacro
Citizen
Posts: 92
Joined: Fri May 30, 2014 2:30 am
Location: Boston, MA, USA
Contact:

Re: math.random not working

Post by MicroMacro »

EDIT: I figured it out. I forgot to call math.randomseed() beforehand. I basically use os.time() to get the seed.
https://github.com/ebernerd- where you can find all my work.
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: math.random not working

Post by slime »

You could also use [wiki]love.math.random[/wiki] instead. LÖVE sets the random seed for love.math.random before love.load is executed, so you don't need to set a seed yourself if you use it.
Bindie
Party member
Posts: 151
Joined: Fri Jan 23, 2015 1:29 pm

Re: math.random not working

Post by Bindie »

slime wrote:You could also use [wiki]love.math.random[/wiki] instead. LÖVE sets the random seed for love.math.random before love.load is executed, so you don't need to set a seed yourself if you use it.
Man, I didn't know that. Thank you.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 69 guests