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

Why does changing pitch to a decimal work in properties but not with math.random?

So, if I go to properties and change the pitch of the sound myself to 1.6 for example, then it works, but with math.random it only changes it to 1 or 2, how do I make it do decimals too?

local randompitch = math.random(1.3,(2.2))
script.Parent.Parent.Sound.Pitch = randompitch
script.Parent.Parent.Sound:Play()

1 answer

✓ Accepted answer

math.random() generates pseudo-random numbers uniformly distributed. Supplying argument alters its behaviour:

math.random() with no arguments generates a real number between 0 and 1. math.random(upper) generates integer numbers between 1 and upper. math.random(lower, upper) generates integer numbers between lower and upper.

= math.random() 0.0012512588885159 = math.random() 0.56358531449324 = math.random(100) 20 = math.random(100) 81 = math.random(70,80) 76 = math.random(70,80) 75

Your numbers aren't integers, if you want to generate a decimal use math.random() or create a larger random number and divide it:

local randompitch = math.random(130, 220)/100

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