First, you can also initialize your array when you declare it and the class should also have a Random
object so you can reuse it rather than creating a new one each time. So, perhaps in the ctor:
rand = new Random();
int[] ImageValues = new int[15]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
You can use a one-liner to randomize/shuffle it:
ImageValues = ImageValues.OrderBy(i => rand.Next()).ToArray();
This will work fine for many cases. The standard Fisher-Yates shuffle is accepted as fast and producing an unbiased order (when used correctly), so in some cases it may be better:
private void ArrayShuffle(int[] items)
{
// uses the Random var declared earlier
int tmp = 0;
int j = 0;
// hi to low, so the rand result is meaningful
for (int i = items.Count() - 1; i >= 0; i += -1)
{
j = rand.Next(0, i + 1); // NB max param is EXCLUSIVE
tmp = items[j];
// swap j and Card i
items[j] = items[i];
items[i] = tmp;
}
}
Either way, once the array is shuffled, you can use it to create a Stack
or Queue
by passing the array in the constructor:
int[] ImageValues = new int[15];
// ...
ArrayShuffle(ImageValues);
Stack<int> mystack = new Stack<int>(ImageValues);
You could just use the array and a variable pointing to the index to use. Using a collection type eliminates the need for that index var and the chance of a bug when it is/is not incremented. For something like a card game, it mimics dealing the next card from the top of the deck:
// ToDo:
// check myStack.Count() if times used is not controlled elsewhere
int nextVal = myStack.Pop();