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

How do I use math.randomseed(tick()) within a function or is that unnecessary?

Answers on a previous question told me to put math.randomseed(tick()) told me to put that line of code at the very top of my script. My question is do I need it in a function or can I put it at Line One?

math.randomseed(tick())-- does it have to be here
local Maps = game.ServerStorage:WaitForChild("Maps"):GetChildren()

function chooseMap()
	--math.randomseed(tick()) or can it/ does it have to be here
	local randomMap = Maps[math.random(1, #Maps)]:Clone()
	print(randomMap.Name)
end

what if i had a while true do loop like this :

--does math.randomseed(tick()) have to be on the first line
-- code in between and naming functions and stuff before the loop, etc.
while true do
	function chooseMap() -- or can it be in the function
end

basically, my question is where can it go

1 comment

2 answers

✓ Accepted answer

Line 1. You should only ever execute randomseed once, at the very start of the game.

Putting it in a function would mean you could accidentally invoke it again.


The first several numbers you get after setting a seed typically aren't very high quality.

You should ask for a few numbers that you'll just throw away to prime it:

math.randomseed(tick())
for i = 1, 10 do
	math.random()
end

Alternatively, you can use a more "chaotic" seed like

math.randomseed(tick() % 1 * 1e6)

4 comments

0

The issue is that it's still spitting out the same map for him. Perhaps a method of seeing if it was the last chosen map

0

"chaotic" ?

0

Chaotic means it adds more possible outcomes like decimals. For your purposes, keep it as whole numbers.

0

tick() only changes by 1 every second. Similar seeds lead to similar initial values out of the random number generator. The alternative changes once every microsecond, so you get a much broader distribution of seeds.

You put it inside the function

	local Maps = game.ServerStorage:WaitForChild("Maps"):GetChildren()
	function chooseMap()
	    math.randomseed(tick()) --< RIGHT HERE
	    local randomMap = Maps[math.random(1, #Maps)]:Clone()
	    print(randomMap.Name)
	end

2 comments

0

putting it at the function's first line doesn't work for me, only 1st line of the script works

0

Did you not read the last reason I downvoted your answer?

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