Make an array of the IDs and then shuffle the array.
Look at my solution in:
playing cards game (make Random 4 groups of Numbers in Main Group)[
^] for shuffling.
Present the records in the shuffled order.
Don't re-use a record, once presented.
Shuffle again for the next sequence of records.
[Edit: MTH]
First get an array of the record objects (or just the IDs). Call it
orderedData
.
Call
var shuffledData = Shuffle(orderedData);
(see below)
Use the values of
shuffledData[0 thru end]
in sequence and it will be the record objects (or IDs, as above) in a random order with no repeats.
Code for Shuffle() converted to generic:
private Random rand = new Random();
public static T[] Shuffle<T>(T[] data)
{
T[] shuffled = new T[data.Length];
shuffled[0] = data[0];
for (int i = 1; i < data.Length; i++)
{
int j = rand.Next(i + 1);
if (j != i)
shuffled[i] = shuffled[j];
shuffled[j] = data[i];
}
return shuffled;
}
[Edit #2: MTH]: I renamed the variable outside of the Shuffle method so there's no confusion about names.