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

How can i make math.random pick a number that is NOT a whole number?

Sorry for the scuffed title, but what i'm trying to do here is pick a random number that isn't a whole number, i checked the wiki and stuff and apparently math.random only produces a number that is a unit of 1, for example, 0,1,2,3,4,5 are whole numbers and 0.5,1.5,2.5,3.5 are not whole numbers.

local Pos = math.random(0.02,0.897)

i did this but of course the number was only ever Zero, (because none of these numbers are higher than 1) so i'm kinda stuck here, this is very important to my game so if anyone can help it'd be really appreciated

(also idk how to mark my question down as answered to yeh)

2 comments

0

you can't, math.random(a,b) can only produce an integer . However, you can just use a larger range, like 20 though 897, then divide it by another number, like 1000

0

you can use Random.new():NextNumber(0.02, 0.897), This is not very good but I think it works.

2 answers

✓ Accepted answer

The number generated by math.random() is always an integer, so if you want a floating-point number (a.k.a a decimal) you would have to perform some math.

To get a random number from 0.02 to 0.897 as according to your code, do math.random(20, 897) and divide that number by 1000 (not 100). This will move the decimal point back three spaces. Here is what the code would look like:

local Pos = math.random(20, 897) / 1000

That's basically it.

1 comment

0

i already figurecd it out a while after posting this question (simple math i'm so stupid) thanks anyways and have a good day :D

local Pos = math.random(20, 897) / 100

that would be your answer, just dont use decimals in it, its really stupid but this is roblox so expect it

1 comment

1

that would range from 0.2-8.97

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