The best practice now is to use the Random class like this:
local rng = Random.new()
local r = rng:NextNumber(5,15)
You can optionally seed Random.new() with a seed value, useful when you want to test with the same random sequence.
You could use math.random() to get the same range, like this:
local r = 5 + 10 * math.random()
But there is no real advantage to this. math.random() internally uses the new Random class, but a global instance of it, and with additional overhead of a conditional to check a FFlag, and another branching to cover the cases where math.random(a,b) is expected to return an integer.
So, basically, don't use math.random. It was patched to use the new PRNG to improve the randomness in existing code (over the old C rand() implementation), but there is no reason to use it for new work.
4 comments
Thanks. :D
This is not generally what people are looking for when they want a float between two values. They usually want ALL floats in the range to be possible outcomes. For example, the first option of math.random(50,150)/10 only has 101 possible values.
@EmilyBendsSpace for one decimal place, it includes all possible outcomes. It is technically impossible to gather all floats since the computer must stop at a certain point within decimals.
@RAYAIN1565 With all due respect, there is a fundamental misunderstanding here. "floats" does not mean real numbers. There are a finite number of floating-point numbers between 0 and 1 (for any format, e.g. IEEE754), and it's possible to make use of them all, but not with the method you've shown.