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

math.random is rounding my numbers, is there a way to get mixed numbers?

Asked by 4 years ago

I'm using math.random() to add values between 1.99 and 3.99 to player.leaderstats.Dollars.Value. However, when using math.random() the numbers were randomized, but came out as integers. Here's the code I first attempted to do this with:

client.leaderstats.Dollars.Value = client.leaderstats.Dollars.Value + math.random(1.99, 3.99)

When I realized that the numbers were being rounded, I added 2 "NumberValue" objects to the script (so... script.min, and script.max). "script.min.Value" was equal to 1.99, and "script.max.Value" was set to 3.99. Here's what I tried:

client.leaderstats.Dollars.Value = client.leaderstats.Dollars.Value + math.random(script.min.Value, script.max.Value)

The numbers were still being rounded to integers. How would I go about fixing this?

0
Multiply math.random() by max, and then clamp it by min. Example: math.clamp(math.random()*3.99, 1.99, math.huge) Fifkee 2017 — 4y
0
What does math.clamp() do? User#28017 0 — 4y
0
clamps a number between min and max. args: n, min, max. if n is less than min, it becomes min. if it is greater than max, it becomes max. otherwise, it returns n. Fifkee 2017 — 4y

2 answers

Log in to vote
2
Answered by
OBenjOne 190
4 years ago

You could just do a random value between 199 cents and 399 Cents and then convert it to Dollars

 Cents = math.random(199, 399)
 Dollars =  Cents/100 
 client.leaderstats.Dollars.Value = client.leaderstats.Dollars.Value + Dollars
0
this one works Fifkee 2017 — 4y
0
That's clever. Thx User#28017 0 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Check out this post : https://scriptinghelpers.org/questions/40983/how-do-i-make-a-mathrandom-with-decimals

Also answered by @OBenjOne

Basically get a random number, divide it by any power of 10.

In this case,

client.leaderstats.Dollars.Value = client.leaderstats.Dollars.Value + math.random(199, 399)/100

Answer this question