Currently math.random() will only return integers if you pass a min and max value. This means if you want to generate a random float you have to do min + (math.random() * (max - min))
There should be a third value that math.random accepts and works similar to JavaScripts toFixed() method. math.random(4, 10, 2) should return a random number between 4 and 10 with 2 decimals of precision, eg 6.79
This will generate a random float between a min and max yea but it still doesn’t have the precision feature. Also the syntax for that isn’t great and neither is having to create/manage a Random instance. Would be much more convenient and easy to use if it was built into math.random which is the function most people use
I just increase the numbers by 10^n times and divide the resulting number by 10^n. (Where n is the decimal precision)
Ex. Instead of math.random(4, 10, 2) I’d do math.random(400, 1000)/100
You can make that yourself for any math function by multiplying the initial parameters with a power of 10 and dividing afterwards:
local function preciseRandom(a: number, b: number, precision: number?): number
local p = precision and 10^precision or 1
return math.random(a*p, b*p)/p
end
print(preciseRandom(2, 1000, 1))
If you plan to run the function very frequently(millions of times) you may want to cache the power results for each precision value to decrease the amount of time the function takes to run.
This will never happen.
The math library is based on the C math library. Under the hood, it’s just calling C code.
That change would break consistency with the C math library.
As already mentioned, the Random class exists to serve this purpose.
Roblox has changed the implementations and contracts of Lua APIs several times. Even math.random isn’t using anything that resembles the PUC-Rio version.
Also people are being caught up on the specific implementation here rather than the actual core feature request of “I want to do something random with precision”. It’s irrelevant whether it’s on math.random or Random class or something new altogether, Roblox wants to know problems more than they want specific suggestions.
To OP, it would help if you provided a use case and explain why it needs to be something the actual API cares about rather than the short functions–Roblox does not like to add things that can be trivially implemented by developers without a clear reason (like perf benefit to a common operation).
My use case was for generating a random price including cents, eg $24.52
It should be built in because when people try do it themselves they are more likely to get the implementation wrong, one of the examples above returns values outside the given range
One of the things I dislike about Luau is the hesitation to implement functionality like this. I very commonly have to write my own small utility functions like this and have a module full of them. All of these functions are built-in in languages like Javascript. They might not be used very often but its so much nicer in Javascript when I need a function like this it’s built-in but in Lua I have to write it myself. For a language like Luau and on a platform like Roblox, there really needs to be more of these utility functions built-in. This isn’t rust where every last bit of performance matters
This is a very specific request and and should probably not be implemented into either math.random or Random.new. It is easy enough to get the desired precision as a separate step.
local rng = Random.new()
function getUpToFiveDollars()
local value = rng:NextNumber(0, 5)
local precisionValue = math.round(value * 100) / 100
return precisionValue
end
print(getUpToFiveDollars())
note: Does not have an even distribution, but would not matter much in this case.
JS is the language of choice for serving practically all users on the internet (check the Sources tab in your browser’s dev console right now and look at all of the JS scripts powering this forum), and it’s the reason why JS and pretty much only JS has built-in precision features - it makes sense, since if you were to present, say, monetary values to a user, odds are you’re going to be doing it with JS. It’s so redundant that adding it to the JS library saves time.
There is simply no need to do so in a language like Lua, whose entire purpose is being lightweight. Clogging the standard library of a language with utility is not necessarily a good thing (looking at you, Python!) and it’s entirely against the M.O. of Lua.
Like others have said, it takes less than 5 lines of code to do what you’re asking in the OP. @NyrionDev’s solution is perfectly reasonable. Additionally, @HugeCoolboy2007 makes a valid point about this being added as functionality to an already existing function used quite frequently for these purposes as opposed to being added as an arbitrary parameter to a function that doesn’t need it.
Numbers in Luau (double precision float) can not exactly represent most decimal numbers. In your example, 24.52 is actually 24.519999999999999573674358543939888477325439453125. These subjectively small errors can build up over time if not handled properly!
You can avoid all of this by counting in cents instead of dollars.