From a deck of 50 need to get sets of 5 as random as possible and as fast as possible. The thought here is to shuffle to get 10 sets of five at a time with no collisions.
Int1 and Int2 will not change during a run. If they do other bad stuff would happen.
public MainWindow()
{
InitializeComponent();
Stopwatch sw = new Stopwatch();
sw.Start();
for(int i = 0; i < 100000; i++ )
{
//Debug.WriteLine(string.Join(", ", NextFive(11, 12)));
int count = NextFive(11, 12).Count();
}
sw.Stop();
Debug.WriteLine(sw.ElapsedMilliseconds.ToString("N0"));
}
Random rand = new Random();
private int[] deckBase;
int nextFiveLastStart = 45;
private IEnumerable<int> NextFive(int int1, int int2)
{
if (int1 == int2)
throw new IndexOutOfRangeException("int1 == int2");
if (int1 > 51 || int2 > 51)
throw new IndexOutOfRangeException("int1 > 51 || int2 > 51");
if (deckBase == null)
{
deckBase = new int[50];
int j = 0;
for (int i = 0; i < 52; i++)
{
if (i == int1 || i == int2)
continue;
deckBase[j] = i;
j++;
}
}
if (nextFiveLastStart >= 45)
{
nextFiveLastStart = 0;
// Yates shuffle
for (int i = 49; i >= 1; i--)
{
int j = rand.Next(i + 1);
if (j != i)
{ // exchange values
int curVal = deckBase[i];
deckBase[i] = deckBase[j];
deckBase[j] = curVal;
}
}
}
else
nextFiveLastStart += 5;
return deckBase.Skip(nextFiveLastStart).Take(5);
}
List
. That way the card isn't in the deck anymore so you can't have collisions and it was shuffled randomly once. What doesint1
andint2
represent? Those aren't very descriptive names? \$\endgroup\$