# Need assistance with making a table with multiple choises, that cant repeat.

So basically i have this:

``````
Table = {Choise1, Choise2, Choise3, Choise4, Choise5}

``````

When used

``````
RandChs = table.Random(Table)

``````

it gets called randomly, but when i call random on it again theres 1/5 chance that same choice will be selected, how do i make it zero chance? How do i make it choose… lets say if table.Random chosen Choice2 how do i make it when called again choose any of the choices except for Choise2?

[lua]Table = {Choise1, Choise2, Choise3, Choise4, Choise5}

newElement = table.Random(Table)

if (oldElement) then
while oldElement == newElement do
newElement = table.Random(Table)
end
end

oldElement = newElement[/lua]

You could probably ignore the if condition as the while loop won’t run anyway if oldElement is nil, but it’s just to clarify.

No repeating values:

``````
2 1 4 5 3 4 3 5 2 1 3 5 4 3 2 5 1 3 1 4 3 1 3 1 5 3 2 3 5 4 2 4 2 3 1 3 5 1 4 2 3 5 4

``````

It works, thanks.
Also. I’ve never used a while loop before. So from what i understand its a loop which loops infinitely until a certain requirement is met. – correct definition?

[lua]while (condition is true) do
– stuff
end[/lua]

Not sure if there is a do while loop in lua, because IMO it’s just as useful as the regular while loop

[lua]
do
– this first part will be executed at least once, no matter what
while (condition is true) – condition is checked at the end, so it always runs at least once
– not sure if this exists in lua
[/lua]

Alright, thanks.

I think you can just use repeat until as a subsitute for do while

Wouldn’t that just increase the chances of the choice not being the same? Or is there a function i dont know about which repeats the stated statement?

For do while, you were talking about it not existing and i was simply stating i think that repeat until is the same almost.

Maybe, it doesn’t matter tho This one works perfectly.

In this situation, it doesn’t matter much, but you should find an alternative solution rather than simply picking another random value from the table until it’s not a value you don’t want. Here, it doesn’t matter at all because only one out of all values in the table is a value you do not want.

But let’s say you are extremely lucky (or unlucky) and the random number generator generates the same value forever… your loop loops forever and you’re screwed. Obviously this will probably never happen, but in other situations, for instance when you want to pick only one of each value in a table only once, you’ll certainly meet that problem.

[editline]10:04AM[/editline]

Also it’s “choice”.

Just a short typing mistake. It picking the same value forever is impossible, tho it is possible to it chooses the same value 100 times, then i might experience a slight lag. good point. So, you got any other solution?

You COULD create a seperate table only containing the values that you want to be picked:
[lua]
Table = {Choice1, Choice2, Choice3, Choice4, Choice5}

if (not something) then
possibleValues = Table
end

newKey = math.random(#possibleValues)
newElement = possibleValues[newKey]
table.Remove(possibleValues, newKey)
something = (#possibleValues >= 0)
[/lua]

Threw this together in 5 minutes off the top of my head.

This will (hopefully) run through the table once and select all the values in random order. After running through the table and selecting each values, it starts over.