- Home /
C# array equal to another array minus one entry.
So, I'm working on a probability-based game in which you are given the ability to select 3 out of 6 chests to open. the three chests that will allow you to move on to the next level are selected at random every time the scene is loaded. the best way I could think of doing this is by having a random.range variable select a number 0-5. this number would be then be applied to an array of choices (the array contains numbers one through six) and the resulting number would be the chest that is correct. the reason I am doing it this way is because I dont want it to select the same chest twice, so I want to remove an option from the array and to do this I want to create a new array that is equal to the old one, minus the previously selected entry. this is the part that I don't know how to do.
chance = Random.Range(0 , 6);
chestNumberOne = choices[chance];
//choices is an array that contains {1,2,3,4,5,6}
anyone have any Ideas on how I can do this?
Answer by AkshayVats · Mar 05, 2015 at 11:21 AM
Using System.Linq;
choices = choices.Where((n, i) => i != chance).ToArray();
Without using linq, you can create a new array and copy values
int[] tmp = new int[choices.length-1];
for (int i = 0, j = 0; i < choices.Length; i++)
if (i == chance) continue;
else tmp[j++] = choices[i];
choices = tmp;