Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make math.random give me a different number?

I am trying to make zombies spawn in random locations (there are 4 at the moment) and they always spawn at the same place. I tried making this custom function to try to make numbers more random but it is not working.

Every time I do a new test session in studio the number changes but it always repeats the same number.

function GetRandom(Num1, Num2)
	local Nums = {}
	math.randomseed(tick())
	for i = 1,100 do
		local Math1 = math.random(Num1,Num2)
		Nums[i] = Math1
	end
	local Math2 = math.random(1,100)
	return Nums[Math2]
end

module.PrepareZombie = function(Name)
	local Zombie = Zombies[Name]:Clone()
	Zombie.Parent = ZombieFolder
	local Spawn = GetRandom(1,#Spawns:GetChildren())
	print(Spawn)
	Spawn = Spawns:GetChildren()[Spawn]
	Zombie.HumanoidRootPart.CFrame = Spawn.CFrame
	ZombieCore.Reincarnate(Zombie)
end

4 comments

0

Roblox naturally gives you random values by generating random seeds.

0

There is no reason whatsoever for that function to exist. Does math.random(1, #Spawns:GetChildren()) not work?

0

As a side note, randomizing a set of random numbers doesn't make your random more random. Random?

0

Are you using the module PrepareZombie function in a server script? If you are, try putting math.randomseed(tick()) at the top instead of in the GetRandom function. Try removing it from the function and putting it in the script you’re using PrepareZombie in as one of the first lines. In all of my scripts needing to use math.random, I always put math.randomseed(tick()) at the top before anything

1 answer

Use math.randomseed( tick() )

math.randomseed() sets the "seed" for math.random. As long as this number is different, your values will be.

tick() fetches the number of seconds elapsed since January 1st 1970. This will always be a different number, so your list of numbers will always be unique.

1 comment

0

He did use math.randomseeed()

This discussion is preserved as it was. New answers, comments, and votes are closed.