Matrix with random numbers - controlled with seed

Hey! I’m currently working on a system that needs to a sort of ‘np.random.randn()’ function as found in python. Particularly, I need the ability to create a matrix of multiple random numbers, with a specific seed - meaning that each number in the matrix will be different, but, if I re-ran the script, the numbers would be the same as last time.

The idea is to basically make a system where you can create multiple random numbers (e.g. an array of numbers) from the same seed. - in python this can be done with Numpy: ‘np.random.randn(X, Y)’, with X and Y being the shape of the matrix.

I don’t think I’m missing anything from Roblox’s built in methods but if I am it would be great if anyone could point out the solution.

Thanks!

What do you mean? The current math.randomseed() function means that if I try and create a matrix from new random numbers, they’re all the same. It’s as if I need to create more then 1 number from 1 execution of math.random(). I’ve even tried the same with Random.new(SEED), and random:NextNumber(), but I still can’t find a solution.

Sorry I don’t understand, not sure if you know what I mean. Loops like that wont help alone. I did think of creating a system where a number is generated, then that number is used as a new seed, then repeating that for all the numbers in the matrix - that way each number stays the same when generated again, but I don’t think its quite how np.random.randn() works, which is what I’m aiming for.

do math.randomseed(SEEDNUMBERHERE) at beggining of the script.

like this:

math.randomseed(SEEDNUMBERHERE)
print("Random number = " … math.random(1,20) … " Seed = " … SEEDNUMBERHERE)

It looks like some messages have got deleted so it I’m missing the context but if you really tried Random.new then I must be misunderstanding what you’re trying to do.

Something like this gives me a consistent random matrix for a given seed.

local generator = Random.new(125)
local matrix = {}
for i=1, 5, 1 do
	matrix[i] = {}
	for j=1, 3, 1 do
		matrix[i][j] = generator:NextNumber()
	end
end

for _, row in pairs(matrix) do
	print(table.concat(row, ", "))
end