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

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

Asked by 5 years ago

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

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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

0
Thank you, I'll try this in my script. NoahsRebels 99 — 5y
Ad

Answer this question