The code below takes a specified number of card decks, and shuffles them according to the Fisher-Yates method. Does this implementation have any bias?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace randomShuffle
{
class Program
{
static void Main(string[] args)
{
List<int> deck = Cards.startDeck(1);
}
}
class Cards
{
public static List<int> startDeck(int numDecks)
{
List<int> initial = new List<int>();
List<int> shuffled = new List<int>();
//set up list of all cards in order
while (numDecks > 0)
{
for (int i = 2; i <= 14; i++) //2-ace(14)
{
for (int j = 0; j < 4; j++) //each card in deck 4 times
{
initial.Add(i);
}
}
numDecks--;
}
//Fisher-Yates method to shuffle
Random r = new Random(DateTime.Now.Millisecond);
int count = initial.Count;
for (int i = 0; i < count; i++) //go through entire unshuffled deck
{
//get random number from 0 to new range of unshuffled deck
int randomDraw = r.Next(0, initial.Count);
//take whatever is drawn and insert at beginning of shuffled list
shuffled.Insert(0, initial[randomDraw]);
//replace the card drawn with the end card
initial[randomDraw] = initial.Last();
//remove the end card from initia deck
initial.RemoveAt(initial.Count - 1);
}
return shuffled;
}
}
}