Random number generator? [SOLVED]

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
Walbal
Prole
Posts: 4
Joined: Fri Jun 09, 2023 3:48 pm

Random number generator? [SOLVED]

Post by Walbal »

random.PNG
random.PNG (9.15 KiB) Viewed 2923 times
I can't get love.math.random() to generate a real random number. It only generates 0. It is in the love.load() function but it does not work. :cry:
Last edited by Walbal on Fri Jun 09, 2023 10:15 pm, edited 2 times in total.
OBEY LOADING |||||
Rigachupe
Party member
Posts: 100
Joined: Fri Jun 18, 2021 11:21 am

Re: Random number generator?

Post by Rigachupe »

Walbal wrote: Fri Jun 09, 2023 4:59 pm random.PNG I can't get love.math.random() to generate a real random number. It only generates 0. It is in the love.load() function but it does not work. :cry:
Is that all the code? Because i use that function and it works exactly that number range i did many times.

Or are you asking for a real number type like 12.45? If that is the case then ehm.. :D i smile because it looks silly that way
local r=love.math.random(1,1000)/100
This would get you a number between 1.00 to 10.00
yal2du
Prole
Posts: 42
Joined: Wed Oct 13, 2021 5:41 pm

Re: Random number generator?

Post by yal2du »

hi, might want to include code that proves your assertion; transcribing code from an image is work and prone to error. here's a piece of test code, also provides platform info (prints to console so run it from shell using lovec.exe):

Code: Select all

local seedlo, seedhi   = love.math.getRandomSeed()
           
local minimum, maximum = 1, 10  -- uniform inclusive range of returned integers

print('platform    ', love.system.getOS())
print('version     ', love.getVersion())
print('seed        ', seedlo, seedhi)
print('range       ', minimum, maximum)

local i = 1
while i <= 20 do
  local rnum = love.math.random(minimum, maximum)
  print('rnum'..tostring(i),rnum)
  assert((rnum >= minimum) and (rnum <=maximum), tostring(rnum)..' is out of range!')
  i = i+1
end

i wasn't able to replicate the issue:

Code: Select all

platform        Windows
version         11      3       0       Mysterious Mysteries
seed            1686337932      0
range           1       10
rnum1   2
rnum2   4
rnum3   3
rnum4   1
rnum5   10
rnum6   2
rnum7   5
rnum8   1
rnum9   8
rnum10  9
rnum11  9
rnum12  10
rnum13  7
rnum14  8
rnum15  3
rnum16  6
rnum17  4
rnum18  7
rnum19  6
rnum20  5
if you need a float, you can't specify the range. it returns (0.0, 1.0) which you can scale and offset to whatever you need:

https://love2d.org/wiki/love.math.random
Walbal
Prole
Posts: 4
Joined: Fri Jun 09, 2023 3:48 pm

Re: Random number generator?

Post by Walbal »

Hi, sorry I did not include code. How should I send it?
OBEY LOADING |||||
Walbal
Prole
Posts: 4
Joined: Fri Jun 09, 2023 3:48 pm

Re: Random number generator?

Post by Walbal »

And also, I am trying to get an int but it always just returns 0. Maybe it is something with the graphics system because I had to use love.graphics.print() instead of just print().
OBEY LOADING |||||
Walbal
Prole
Posts: 4
Joined: Fri Jun 09, 2023 3:48 pm

Re: Random number generator?

Post by Walbal »

Oh, it was the graphics system, thank you guys so much for helping!
OBEY LOADING |||||
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Amazon [Bot] and 6 guests