How do you make math.random() print decimals?

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:
image
but it still generates a whole number. Is it possible to make it generate a decimal?

18 Likes

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

15 Likes

You could use Random.new():NextNumber()
Example:

local random = Random.new()
print(random:NextInteger(1, 10)) -- 4
print(random:NextNumber(1, 10)) -- 6.2352857523455
73 Likes

You could also do

local function decimalRandom(minimum, maximum)
    return math.random()*(maximum-minimum) + minimum
end

print(decimalRandom(0.2, 1.8))
19 Likes

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?

11 Likes

Late 2017:

4 Likes

You can use this:

print(math.random())

It prints a random decimal between 0 and 1.

7 Likes

maybe you can just use math.random(0, 100) / 10

8 Likes

I was stuck with the same thing. Thanks!

4 Likes