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

How does math.random work here?

Asked by
vinboi 0
8 years ago

From http://wiki.roblox.com/index.php?title=Making_an_Explosion_Course

I don't understand how adding random number from 1-3 can make different parts explode at different times. If I "remove number = math.random(1,3)" and the if condition that checks for 1, my parts would explode at the same time. How does it change the behavior of my "ExplodePart"s from exploding uniformly at the same time to exploding at different times? How does math.random choose which parts explode at random times rather than all parts explode at random times?

function ExplodePart(part)
    number = math.random(1,3)
    if number == 1 then
        explosion = Instance.new("Explosion", game.Workspace)
        explosion.Position = part.Position
    end
end

children = game.Workspace:GetChildren()
while true do
    for _, child in ipairs(children) do
        if child.Name == "ExplosionPart" then
            ExplodePart(child)
        end
    end
    wait(1)
end

1
Well, it has a one in three chance to explode, because math.random(1,3) and then it has a one in three chance of being one. So it will constantly do this, and if every single time it''s not one, then it won't explode. theCJarmy7 1293 — 8y
0
you could also put this instead of line 2. wait(math.random(1,3)) LostPast 253 — 8y
0
That's not quite how it works Sporkyz BlackJPI 2658 — 8y
0
math random 1,3 means 1 incuded 3 excluded. scottmike0 40 — 8y

Answer this question