I wan’t to make it so that math.random generates a specific decimal, but I can’t seem
how to get it to.
This is what I have tried doing:
but it still generates a whole number. Is it possible to make it generate a decimal?
If you want to get a random number between .2 and 1.8 you can do:
print(math.random(2,18)*.1)
math.random can only take whole numbers (no decimals)
so we do math.random(2,18)
to find a number between 2 and 18 then move the decimal over 1 space by multiplying it by .1
You could use Random.new():NextNumber()
Example:
local random = Random.new()
print(random:NextInteger(1, 10)) -- 4
print(random:NextNumber(1, 10)) -- 6.2352857523455
You could also do
local function decimalRandom(minimum, maximum)
return math.random()*(maximum-minimum) + minimum
end
print(decimalRandom(0.2, 1.8))
Am I the only person that never realized Random.new()
was a thing? For years I’ve been getting non integer random numbers by dividing for the precision needed like this:
local r = math.random(500, 1000)/100 --r will now be [5, 10] with two decimals of precision
Anybody know when this was introduced, or has Ranom.new()
always been out there and I just never realized?
Late 2017:
You can use this:
print(math.random())
It prints a random decimal between 0 and 1.
maybe you can just use math.random(0, 100) / 10
I was stuck with the same thing. Thanks!