How do I add a decimal point to a number using a script?

So basically, I was making a math.random script for a gui, but I want it to be in scale and not offset, so I wanted to add a decimal point to the math.random script. So, I decided to try adding one to the number, but I always get an error and I keep doing it wrong. How could I add a decimal point to a number inside a script.

For example :

local r = math.random(1,2)
print(. + r)

How could I do this inside a script?

You can instead use the call math.random() (with no arguments) to generate a number in the range of [0, 1). You can also just divide the result by 10 or 100 or whatever number you please. For instance

math.random(0, 100)/100
math.random(0, 10)/10

Or just in general math.random(min, max)/max

3 Likes

Thanks for the help!

30 characters

Adding onto this for future reference, if you wanted a random number between 0.1 and 0.2, simply use those numbers in the math.random function!

math.random(0.1,0.2)

Nevermind. See below. (Thanks @XAXA. I didn’t realise!)

That’s not how math.random works with two parameters. math.random only generates integers when you call it with 1 or 2 arguments, so those will just get rounded down. You’re pretty much just calling math.random(0, 0), which will just generate 0s.

If you want to generate a “decimal” number within an interval, I would recommend using the Random object

local r = Random.new()
print(r:NextNumber(0, 10)) -- 6.5671190270581
print(r:NextNumber(0, 10)) -- 8.6838278457998
print(r:NextNumber(0, 10)) -- 3.1862607605153
print(r:NextNumber(0, 10)) -- 9.6170438609961
-- ... you get the idea. It only prints decimal numbers between 0 and 10
7 Likes