Add parameter to Random methods to generate a sequence of numbers at once

As a Roblox developer, it is tedious to write statements to produce a random vector or a table of random values.

It would be really awesome if NextInteger and NextNumber had an optional third argument that indicates how many numbers to return as a Tuple:

Tuple<int> Random:NextInteger(int min, int max, int amount=1)
Tuple<double> Random:NextNumber(double min=0, double max=1, int amount=1)

It would be useful in cases like these:

local x = rng:NextNumber(-100, 100)
local y = rng:NextNumber(-100, 100)
local z = rng:NextNumber(-100, 100)
local vec = Vector3.new(x, y, z)

where the code can be shortened to:

local x, y, z = rng:NextNumber(-100, 100, 3)
local vec = Vector3.new(x, y, z)

or even:

local vec = Vector3.new(rng:NextNumber(-100, 100, 3))

If Roblox were able to address this issue, writing code to produce sequences or vectors of random values would be more convenient to write.

5 Likes

In the meantime, you can add these functions to your game – probably in a utilities module of some sort:

local function MultiNextInteger(rng, a, b, count)
	local t = {}
	for i = 1, (count or 1) do
		t[i] = rng:NextInteger(a, b)
	end
	return unpack(t)
end

local function MultiNextNumber(rng, a, b, count)
	local t = {}
	for i = 1, (count or 1) do
		t[i] = rng:NextNumber(a, b)
	end
	return unpack(t)
end

local vec = Vector3.new(MultiNextNumber(rng, -100, 100, 3))

Here’s another fun one:

local function ManyNextInteger(rng, ...)
	local t = {}
	local count = select('#', ...)
	for i = 2, count, 2 do
		t[i/2] = rng:NextInteger(select(i - 1, ...))
	end
	return unpack(t)
end

local function ManyNextNumber(rng, ...)
	local t = {}
	local count = select('#', ...)
	for i = 2, count, 2 do
		t[i/2] = rng:NextNumber(select(i - 1, ...))
	end
	return unpack(t)
end

local vec = Vector3.new(ManyNextNumber(rng, -100,100, -50,50, -100,100))
3 Likes