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

math.random(0.5, 1) only returning 0 and 1? [SOLVED]

Asked by 2 years ago
Edited by T3_MasterGamer 2 years ago

So I have a math.random variable and it should print values between 0.5, 0.6, etc. but it only prints 0 and 1. Here's my code:

1local size = math.random(0.5, 1)
2print(size)

Edit: I figured it out. So apparently, math.random rounds numbers. So I just did math.random(5, 10) / 10, and that worked.

1 answer

Log in to vote
1
Answered by 2 years ago

math.random only returns integers, if you want to have decimals then I suggest:

1local random = Random.new()
2local random_integer = random:NextInteger(1, 10)
3local random_number = random:NextNumber(1, 10)
4 
5print(random_integer) -- without decimals (6)
6print(random_number) -- with decimals (6.1)
1
Not all the time. You see, math.random() only return decimals when it is not filled (meaning 0 and 1 are the parameters). It will only return integers if the difference between the two parameters are more than one. T3_MasterGamer 2189 — 2y
0
What do you mean by that? xXLegendGamerz16Xx 231 — 2y
Ad

Answer this question