Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to get decimal outputs from math.Random?

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 comment

1

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

1 answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.