Random.new() returning the same number

function module:RandomKey(Table,seed : number)
	local rng = Random.new(seed or 55)
	assert(typeof(Table) == "table",script.Name.." Error: Function RandomKey did not receive a table.")
	local selectedIndex_Key = rng:NextInteger(1,#Table)
	local selectedValue = Table[selectedIndex_Key]
	local final = {
		Key = selectedIndex_Key,
		Value = selectedValue
	return final

I have a table for the argument, which is a dictionary like this:

local module = require(script.ModuleScript)

local a = {"a","b","c"}

while wait(1) do
	local random = module:RandomKey(a)
	print(random["Key"], random["Value"])

However, everytime it prints the key and value, it’s always the same. It keeps printing 3 c and not the other contents inside the table. Am I doing something wrong?

EDIT: Turns out it’s the seed 55 that ruined the problem, so I didn’t add any seeds inside the Random.new() argument and it solves the problem, however whenever I add a random number, it will only keep printing that one number. Anyone can explain this?

Because if you don’t pass a seed, its automatically setting the seed to 55 which makes the random number that’s generated always the same.

Try using tick() instead of 55.

Edit: You don’t need the seed parameter it is optional. But you cannot pass a nil value. So just do

local rng
if seed then
    rng = Random.new(seed)
    rng = Random.new()
1 Like

I could’ve just did the same thing using the or operator.

This worked. Thanks!