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

Math.Random doesn't function properly?

Asked by 6 years ago

I've heard of a solution that uses math.randomseed and the tick function, however I don't fully understand it and I don't know how to implement it into this code. The problem is that it chooses the same result everytime.

local Maps = game.ServerStorage:WaitForChild("Maps"):GetChildren()

function chooseMap()
    local randomMap = Maps[math.random(1, #Maps)]:Clone()
    randomMap.Parent = workspace.MapHolder
    print(randomMap.Name)
end

chooseMap()

If anyone has any better advice on how to code this more properly and more advanced please let me know in your answer or comment.

0
push all the code down by one line then on line one do math.randomseed (tick()) and thats it abnotaddable 920 — 6y
0
Also, I tried it by running it in studio but the same issue arises however with a different result Subaqueously 11 — 6y

2 answers

Log in to vote
1
Answered by 6 years ago

So, the way math.randomseed works, you need to kinda know how math.random works. You might think "Oh yeah, it generates a random number from the parameters." This is incorrect. It gets based off what the seed is, but I have not been studying how math.random works so I cannot tell you how it gets a number of the seed. math.randomseed changes the seed.

Here is a good example of math.randomseed

math.randomseed(os.time()) --it just needs a random number, should not be generated with math.random or seed will be same in every server
local Print()
    print(math.random(1, 10)
end

Print()
Print()
Print()
Print()
Print()
Print()
Print()

However, here is a bad example.

local Print()
    math.randomseed(os.time())
    print(math.random(1, 10)
end

Print()
Print()
Print()
Print()
Print()
Print()
Print()

Why is this bad? Well, if we fire this function a lot within a second, we will get the same number printed.

Hope this helps!

0
what is os.time Subaqueously 11 — 6y
1
os.time[edit] int os.time() Description: Returns how many seconds since the epoch (1 January 1970, 00:00:00), under UTC time. hiimgoodpack 2009 — 6y
Ad
Log in to vote
-1
Answered by
Edenojack 171
6 years ago

With math.randomseed, all you need to do is add math.randomseed(tick()) to your code, litterally just in the line above randomMap. By doing this, you change the current "seed" of random possibilites.

Take for example a minecraft world, its all randomly generated around a "seed". While it does something very different with that seed, the principles remain v similair.

0
What is the tick function there for Subaqueously 11 — 6y
0
Also, I tried it by running it in studio but the same issue arises however with a different result Subaqueously 11 — 6y
0
the tick() is just your time in seconds (day,month,year) its a really long number that can set a random time by Edenojack 171 — 6y
0
How many maps do you have? Edenojack 171 — 6y
View all comments (3 more)
0
I have 9 maps within game.ServerStorage.Maps Subaqueously 11 — 6y
0
no that means you have the same choice every time... hiimgoodpack 2009 — 6y
0
Also, tick is not your time in seconds from the start of the year. hiimgoodpack 2009 — 6y

Answer this question