Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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)

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 theking48989987 2147 — 5y
0
you can use Random.new():NextNumber(0.02, 0.897), This is not very good but I think it works. yHasteeD 1819 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

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.

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 FlabbyBoiii 81 — 5y
Ad
Log in to vote
1
Answered by
len_ny 8
5 years ago
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
that would range from 0.2-8.97 theking48989987 2147 — 5y

Answer this question