public void TestRandomNumberGenerator() { //Since it's impossible to deterministically test generating a random number, we aren't going to do that. //We can, however, test a bunch of iterations to verify that the number is within the acceptable range. for (int i = 0; i < 100000; i++) { var randomInt = RandomNumberUtils.GetRandomInt(0, 51); Assert.IsTrue(randomInt >= 0); Assert.IsTrue(randomInt <= 51); } }
/// <summary> /// Shuffles an arbitrary deck of cards /// </summary> /// <param name="listToShuffle">The list of cards to shuffle</param> /// <returns>A shuffled list of cards</returns> public static List <Card> ShuffleCards(List <Card> listToShuffle) { //Null check if (listToShuffle == null || !listToShuffle.Any()) { throw new ArgumentNullException("Please pass in a valid deck!"); } //Basic idea here is that if we run through the deck and randomly swap each card with another card, //and repeat the process to increase randomness, we can shuffle the deck to an acceptable level of randomness //Adding the ability to run through the deck in multiple iterations allows for extra randomness for (int iteration = 0; iteration < 5000; iteration++) { //Here we're looping through each card in the deck and randomly swapping it with another card. Note that //swapping a card with itself is statistically valid. for (int i = 0; i < listToShuffle.Count; i++) { listToShuffle.Swap(i, RandomNumberUtils.GetRandomInt(0, listToShuffle.Count - 1)); } } return(listToShuffle); }