Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

This question already has an answer here:

Math.Random doesn't function properly?

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

Marked as Duplicate by hiimgoodpack, lukeb50, Void_Frost, and Goulstem

This question has been asked before, and already has an answer. If those answers do not fully address your question, then please ask a new question here.

Why was this question closed?

2 answers

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
6 years ago
Edited 6 years ago

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)
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 Edenojack 171 — 6y
0
"chaotic" ? Subaqueously 11 — 6y
0
Chaotic means it adds more possible outcomes like decimals. For your purposes, keep it as whole numbers. Viking359 161 — 6y
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. BlueTaslem 18071 — 6y
Ad
Log in to vote
-1
Answered by
Edenojack 171
6 years ago

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
0
putting it at the function's first line doesn't work for me, only 1st line of the script works Subaqueously 11 — 6y
0
Did you not read the last reason I downvoted your answer? hiimgoodpack 2009 — 6y