Decimals in random percentage

I’ve created a command for mods only that uses a low random percentage generator. I’d like a range of decimals to show up in the percentage. However, it seems to be stuck and always puts .001 at the end of the percentage and no other variation of decimal.
here’s what the command looks like:

!commands add !modding -ul=moderator $(user) is modding at $(eval min = 0.001; max = 25.0; Math.floor(Math.random() * (max - min + 1)) + min;)% today o7

so for example the outcome always looks like:
ehayys is modding at 16.001% today o7
should I be inputting the min/max number differently? thanks!

Hey @ehayys!

It’s not really that you need to input the min/max numbers differently, it’s that it’s not the correct way to generate a random number with 10^-3 precision…
Math.floor() will always give you an integer (with no decimals), yet you need it for that command otherwise you’ll get a lot of decimals: 15 to be precise.
You always get .001 added because it comes after the Math.floor(), you simply add 0.001 to an integer.

So what’s the solution? Well, it’s simple when you’re used to such logic: generate a random number that can go up to 10^4 (25 is 2.5 x 10^1) and then divide it by 10^3! Like this:

!addcom !modding -ul=moderator $(eval min = 1; max = 25000; `$(user) is modding at ${(Math.floor(Math.random() * (max - min + 1)) + min) / 1000}% today o7`;)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.