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()
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()
There is no need to build an intermediate table though; you can simply write a recursive function to generate the vararg:
local function rand(n)
if n == 1 then return math.random() end
return math.random(), rand(n-1)
end
shorter (and possibly faster); also doesn't create a garbage table; even better, you could generalize this:
local function rep(func, n)
if n == 1 then return func() end
return func(), rep(func, n-1)
end
then use as follows:
local x, y, z = rep(math.random, 3)
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)
math.random() only returns one number, but you can just set one variable to be the result of math.random(), then set the others based on some mathematical operation of the variable you assigned to be math.random().
For instance:
local x = math.random()
local y = x * 5
local z = y - x
y and z are still "random" values because they're indirectly generated from math.random().