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

math.random with tables seems to be giving similar number after picked?

I have a round-based game I was working on and sometimes you can get special rounds. I pick them out using this:

Chance = {1, 2, 3, 4, 5}
Choice = (Chance[math.random(#Chance)])

if Choice == 1 then
   -- do something
elseif Choice  == 2 then
 -- do something else

-- and so on

Although lets say "Choice" happens to be 2. If it picks 2, it seems like it picks 2 more often for a few turns. If it were to pick one, it'd pick one more often for a few turns. I was wondering if there was a better way to do this or if I just happened to be getting the same number a few times in a row.

1 answer

✓ Accepted answer

Use "math.randomseed", this randomizes the number table of math.random.

math.randomseed(tick())
Chance = {1, 2, 3, 4, 5}
Choice = (Chance[math.random(#Chance)])

if Choice == 1 then
   -- do something
elseif Choice  == 2 then
 -- do something else

-- and so on

Read more on random number generation here: https://developer.roblox.com/en-us/articles/Random-Number-Generation

1 comment

0

Thank you, I'll try this in my script.

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