How to prevent math.random from picking the same map

What I do with random selection for my game is have a “Reference” table, which is all the items you want to randomize, and then create a second table to basically read from the reference table and then be used for actually picking the random values.

local referenceTable = {
"String",
"Hello",
"Cool"
}
local valuesToChoose = {}

After that, you want to basically:

  • Clone every item into the second (empty) table (so it looks identical)
  • Before choosing a value, check to make sure the second (to choose from) table isn’t empty. If it is empty, reload it from the reference table
  • Then, choose your random value, and then delete it from the second (choosing) table

I use this because it seems to be the easiest way to visualize what hasn’t been chosen, and it’s pretty easy to implement.

1 Like