Does math.RandomSeed influence Random.new?

So I have this function where I restock a dictionary with one new thing from each rarity everytime and I wanna make it so every server has the same thing so Im using math.randomseed but my function looks like this:

	local keys = {}
		for key, _ in pairs(rarityTable) do
			table.insert(keys, key)
		end

		if #keys == 1 then
			local onlyKey = keys[1]
			return rarityTable[onlyKey]
		end

		local position = Random.new():NextInteger(1, #keys)
		local key = keys[position]

		table.insert(towerRoster,rarityTable[key])

I use Random.new() but im not sure if its “compatible” with math.randomseed, is it compatible or should I just change it to math.random, any help is appreciated, thanks!

2 Likes

Random.new() has seed as an optional parameter, so you can do the following:

local seed = 123456789 --example

local r1 = Random.new(seed):NextInteger(1, #keys)
local r2 = Random.new(seed):NextInteger(1, #keys) 

print(r1 == r2) --true they are the same number

PS: The only case in which this may return a different value but with the same seed is if the amount of keys isn’t equal to its client/server counter part.

1 Like

The Random.new() and math.random() functions serve the same purpose of generating random numbers, but they are different implementations. When you use Random.new(), you are creating a new instance of the Random class, which provides its own independent random number generator.

If you’re looking to use math.randomseed() to ensure that every server generates the same sequence of random numbers, you should use math.random() instead of Random.new().

Here’s how you can modify your code to use math.random() and math.randomseed():

luaCopy code

-- Set the seed value to ensure consistent random number generation
math.randomseed(12345) -- You can use any seed value you prefer

local keys = {}
for key, _ in pairs(rarityTable) do
    table.insert(keys, key)
end

if #keys == 1 then
    local onlyKey = keys[1]
    return rarityTable[onlyKey]
end

local position = math.random(1, #keys)
local key = keys[position]

table.insert(towerRoster, rarityTable[key])

By setting the seed using math.randomseed(), you ensure that the sequence of random numbers generated by math.random() will be the same across all servers, which helps achieve consistency in your game’s functionality.

1 Like

math.randomseed() only influences math.random(), making it more accurately random.

Random.new() also has its own math.randomseed() feature—the seed argument in Random.new().

local seed = 1000 -- you can use any number you want

-- math.random() version
math.randomseed(seed)
math.random(1, 10)

-- Random.new() version
Random.new(seed):NextInteger(1, 10)

There’s no difference between the two. You can use math.random() and math.randomseed() if you want shorter letters in a line, or Random.new() if you want shorter lines in a code.

1 Like

ik that this script is more complex but you can adjust more

                                                                                                                                                              -
  • Set the seed value to ensure consistent random number generation

math.randomseed(12345) – You can use any seed value you prefer

local keys = {}
for key, _ in pairs(rarityTable) do
table.insert(keys, key)
end

if #keys == 1 then
local onlyKey = keys[1]
return rarityTable[onlyKey]
end

local position = math.random(1, #keys)
local key = keys[position]

table.insert(towerRoster, rarityTable[key])

I think im gonna use math.random because apparently it’s more reliable, thanks everyone!

1 Like

i am glad that we could help you

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.