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

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

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:

local size = math.random(0.5, 1) 
print(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

✓ Accepted answer

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

local random = Random.new()
local random_integer = random:NextInteger(1, 10)
local random_number = random:NextNumber(1, 10)

print(random_integer) -- without decimals (6)
print(random_number) -- with decimals (6.1)

2 comments

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.

0

What do you mean by that?

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