No-Repeat Method for Tables

Hello everyone! 

Just asking for some great ideas to hone my coding skills

Ok so I have this table let’s name myLists:

local myLists = {

“first_entry”,

“second_entry”,

“third_entry”

}

local myAnswer = math.random(1,#myLists)

when myAnswer gets a random value and the player manages to answer it, let’s use “second_entry” for example, i dont want it to be a choice for the next round for math.random. I want math.random to skip it if it encounters “second_entry” in the next round. . there are two ways to do this. delete it from the table after the player is able to answer the question(second_entry) or make a ‘skip’ function so math.random will pick another data if it encounters ‘second_entry’ in the next round…

Does anyone have a suggestion on how to do this properly??? gimme a tidbit of code for reference >:DD
many thanks in advance. ROCK ON!

Removing it from the table is one easy method.  You’d probably just want to make a copy of the table first, so that you’re not modifying the original version of the table.

Alternatively, you could pre-process it by assigning a random number to each item, create a new table that’s sorted based on that number, and then just iterate through the new table one at a time (it’ll be random, since you sorted it based on a random number).

  • Andrew

ok will try the alterative pre-process…sounds good to me. thanks :slight_smile:

Search the forums for “shuffle table” or something like that.  There are some very easy to implement routines that will shuffle your table and then just loop over it from 1 to #tablesize and you get each entry once but they are randomly sorted.

@Rob Miracle

thnx rob.! you’re referring to ‘fisher-yates’ shuffle, ya?? :slight_smile:

Well @reyanthohyrenacia, there are multime methods, I use one I stole from a forum post that seemed simple enough to understand that apparently was stolen from another forum post.  I have no idea what its called other than in my “utility.lua” file there is a function table.shuffle() block of code :slight_smile:

Removing it from the table is one easy method.  You’d probably just want to make a copy of the table first, so that you’re not modifying the original version of the table.

Alternatively, you could pre-process it by assigning a random number to each item, create a new table that’s sorted based on that number, and then just iterate through the new table one at a time (it’ll be random, since you sorted it based on a random number).

  • Andrew

ok will try the alterative pre-process…sounds good to me. thanks :slight_smile:

Search the forums for “shuffle table” or something like that.  There are some very easy to implement routines that will shuffle your table and then just loop over it from 1 to #tablesize and you get each entry once but they are randomly sorted.

@Rob Miracle

thnx rob.! you’re referring to ‘fisher-yates’ shuffle, ya?? :slight_smile:

Well @reyanthohyrenacia, there are multime methods, I use one I stole from a forum post that seemed simple enough to understand that apparently was stolen from another forum post.  I have no idea what its called other than in my “utility.lua” file there is a function table.shuffle() block of code :slight_smile: