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

Math.Random doesn't function properly?

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.

2 comments

0

push all the code down by one line then on line one do math.randomseed (tick()) and thats it

0

Also, I tried it by running it in studio but the same issue arises however with a different result

2 answers

✓ Accepted answer

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!

2 comments

0

what is os.time

1

os.time[edit] int os.time() Description: Returns how many seconds since the epoch (1 January 1970, 00:00:00), under UTC time.

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.

7 comments

0

What is the tick function there for

0

Also, I tried it by running it in studio but the same issue arises however with a different result

0

the tick() is just your time in seconds (day,month,year) its a really long number that can set a random time by

0

How many maps do you have?

0

I have 9 maps within game.ServerStorage.Maps

0

no that means you have the same choice every time...

0

Also, tick is not your time in seconds from the start of the year.

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