Skip to content

Instantly share code, notes, and snippets.

@Epyo
Created May 7, 2020 05:16
Show Gist options
  • Save Epyo/1d4d51a39b3426229973bbf832009579 to your computer and use it in GitHub Desktop.
Save Epyo/1d4d51a39b3426229973bbf832009579 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GoFishProblem
{
public class Program
{
public static void Main(string[] args)
{
Random random = new Random();
int inputNumPlayers = 12;
int inputNumSimulations = 10000000;
int resultingNumSimulationsWhereNoPairs = 0;
for (int eachSimulation = 0; eachSimulation < inputNumSimulations; eachSimulation++)
{
// Construct random deck:
List<int> originalDeck = new List<int>();
List<int> randomDeck = new List<int>();
for (int i = 0; i < 48; i++)
{
originalDeck.Add(i / 2);
}
while (originalDeck.Count > 0)
{
// Pick random card from originalDeck, move to randomDeck:
int randomSlotFromOriginalDeck = random.Next(0, originalDeck.Count);
randomDeck.Add(originalDeck[randomSlotFromOriginalDeck]);
originalDeck.RemoveAt(randomSlotFromOriginalDeck);
}
// Deal to players, as soon as a player gets the same card twice, stop:
List<List<int>> playersHands = new List<List<int>>();
bool atLeastOnePair = false;
for (int eachPlayer = 0; eachPlayer < inputNumPlayers; eachPlayer++)
{
var thisPlayersHand = new List<int>();
playersHands.Add(thisPlayersHand);
//Console.WriteLine();
for (int eachHandSlot = 0; eachHandSlot < 4; eachHandSlot++)
{
// Pick random card from randomDeck:
int randomSlotFromDeck = random.Next(0, randomDeck.Count);
int chosenCardValue = randomDeck[randomSlotFromDeck];
//Console.WriteLine(chosenCardValue);
// Does this player have this card value already? If so, we're done.
if (thisPlayersHand.Contains(chosenCardValue))
{
atLeastOnePair = true;
break;
}
// Otherwise move the card from deck to this players hand:
thisPlayersHand.Add(chosenCardValue);
randomDeck.RemoveAt(randomSlotFromDeck);
}
if (atLeastOnePair)
break;
}
if (!atLeastOnePair)
{
//Console.WriteLine("NO PAIRS");
resultingNumSimulationsWhereNoPairs++;
}
else
{
//Console.WriteLine("PAIR OCCURRED");
}
//Console.ReadLine();
}
Console.WriteLine((100 * resultingNumSimulationsWhereNoPairs / (double)inputNumSimulations) + "% chance of nobody having any pair");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment