Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to get decimal outputs from math.Random?

Asked by 3 years ago

The title speaks for itself, im attempting to use math.Random, however I need to use precise numbers and whenever I use the function it appears to 'simply' my numbers (i.e il give math.random 0, 5.3614139556885 as arguments, and il get back 3.) Is there any way to get more precise numbers from math.Random?

1
You can also use Random : Random.new():NextNumber(0, 5.3614139556885) https://developer.roblox.com/en-us/api-reference/datatype/Random imKirda 4491 — 3y

1 answer

Log in to vote
0
Answered by
yx10055 57
3 years ago
function RandomWithDecimals(x, y, decimalRange)
    return math.random(x * math.pow(10, decimalRange), y * math.pow(10, decimalRange))/math.pow(10, decimalRange)
end

print(RandomWithDecimals(1, 10, 5))

You do math.random normally except there is a third argument which is a decimal range. That is how many digits you want your decimal to have. What I mean is like: 0.2398 has a decimal range of 4, 0.239888 has a decimal range of 6, 0.1 has a decimal range of 1

Ad

Answer this question