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

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

Asked by
lucas4114 607 Moderation Voter
9 years ago

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

Log in to vote
1
Answered by 9 years ago

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
Ad

Answer this question