Search code examples
luaroblox

How to give X, Y, Z different values from the same math.random() function?


Basically what i want is:

local X, Y, Z = math.random()

Without assigning each value to math.random():

local X, Y, Z = math.random(), math.random(), math.random()

Solution

  • You can write a function that does that for you:

    local function rand(n)
        local res = {}
        for i = 1, n do
            table.insert(res, math.random())
        end
        return table.unpack(res)
    end
    

    And call it like that:

    local X, Y, Z = rand(3) -- Get 3 random numbers
    print(X, Y, Z)