Need help with random.math

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
sebadiazssj3
Prole
Posts: 4
Joined: Sun Sep 30, 2018 6:50 pm

Need help with random.math

Post by sebadiazssj3 »

I'm trying to get diferent numbers every time opening love but always get the same ones

Need help please!!

The code:

function love.load(math.getRandomSeed())
numero1 = math.random(1, 48)
numero2 = math.random(1, 48)
numero3 = math.random(1, 48)
numero4 = math.random(1, 48)
numero5 = math.random(1, 48)
end
function love.update()
if numero2 == numero1 then
numero2 = math.random(1, 48)
end
if numero3 == numero1 then
numero3 = math.random(1, 48)
end
if numero3 == numero2 then
numero3 = math.random(1, 48)
end
if numero4 == numero1 then
numero4 = math.random(1, 48)
end
if numero4 == numero2 then
numero4 = math.random(1, 48)
end
if numero4 == numero3 then
numero4 = math.random(1, 48)
end
if numero5 == numero1 then
numero5 = math.random(1, 48)
end
if numero5 == numero2 then
numero5 = math.random(1, 48)
end
if numero5 == numero3 then
numero5 = math.random(1, 48)
end
if numero5 == numero4 then
numero5 = math.random(1, 48)
end
end
function love.draw()
love.graphics.print(numero1, 100, 200)
love.graphics.print(numero2, 150, 200)
love.graphics.print(numero3, 200, 200)
love.graphics.print(numero4, 250, 200)
love.graphics.print(numero5, 300, 200)
end
User avatar
EngineerSmith
Prole
Posts: 38
Joined: Thu Dec 02, 2021 11:38 am
Contact:

Re: Need help with random.math

Post by EngineerSmith »

You should be using `love.math.random` than lua's random. Love.math.random is seeded for you each time you run the program, so you get different values. Whilst math.random isn't seeded, and you must seed it yourself.
Ross
Citizen
Posts: 98
Joined: Tue Mar 13, 2018 12:12 pm
Contact:

Re: Need help with random.math

Post by Ross »

You need to set the random seed with 'math.randomseed' to get different random numbers from 'math.random'.

I usually do:

Code: Select all

math.randomseed(love.timer.getTime() * 10000)
...in love.load (you just need call it once).
sebadiazssj3
Prole
Posts: 4
Joined: Sun Sep 30, 2018 6:50 pm

Re: Need help with random.math

Post by sebadiazssj3 »

This is the code that i was acctualy using

function love.load()
numero1 = math.random(1, 48)
numero2 = math.random(1, 48)
numero3 = math.random(1, 48)
numero4 = math.random(1, 48)
numero5 = math.random(1, 48)
end
function love.update()
if numero2 == numero1 then
numero2 = math.random(1, 48)
end
if numero3 == numero1 then
numero3 = math.random(1, 48)
end
if numero3 == numero2 then
numero3 = math.random(1, 48)
end
if numero4 == numero1 then
numero4 = math.random(1, 48)
end
if numero4 == numero2 then
numero4 = math.random(1, 48)
end
if numero4 == numero3 then
numero4 = math.random(1, 48)
end
if numero5 == numero1 then
numero5 = math.random(1, 48)
end
if numero5 == numero2 then
numero5 = math.random(1, 48)
end
if numero5 == numero3 then
numero5 = math.random(1, 48)
end
if numero5 == numero4 then
numero5 = math.random(1, 48)
end
end
function love.draw()
love.graphics.print(numero1, 100, 200)
love.graphics.print(numero2, 150, 200)
love.graphics.print(numero3, 200, 200)
love.graphics.print(numero4, 250, 200)
love.graphics.print(numero5, 300, 200)
end


Not function love.load(math.getRandomSeed()) sorry the mistake
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Need help with random.math

Post by zorg »

the answer is still the same, either set the random generator's seed as per an earlier post, or just use love.math.random
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Roland Chastain and 37 guests