Hi, so I’m making a script that will automatically choose a random upgrade using math. random(). However, the numbers are returning a lot of duplicates so how would I prevent the same number from being returned twice in a row.
randomseed to have more randomized variant
math.randomseed(tick())
-- now use random function
math.random(Start, End)
and in case they are the same in a row u will have to check that with if
statement
Thanks, It Worked! But I don’t really understand what the first function does
Random numbers aren’t really random. Computers are logical machines and they create only patterns. Random functions use a convoluted series of math operations to make numbers appear random. As part of this process, they take a second input, the seed. The seed is provided only once and helps to determine the course that the “random” numbers will take. If the seed is ever the same twice, then the course of the random numbers will all be the same.
The seed provided here is tick(), which is Unix time in seconds to guarantee the seed will never be the same twice. However, the seed might be similar enough that the first one or two numbers might be the same between servers, but that usually doesn’t matter.
A lot of programs use an irrational number (such as pi) to create random numbers.
randomseed
basically picks a position of the “number” (pi in this case). Pi is 3.141… and a randomseed
of 2 would start from the second digit of pi (1), rather than the first (3).
I believe randomseed
automatically sets to how long the game is running, which explains why it’s repetitive. I’m not 100% certain so don’t just take my word for it.