Probability of drawing an ace after 13 draws with Monte carlo simulation

127 Views Asked by At

**A player is randomly dealt a sequence of 13 cards from a deck of 52-cards. All sequences of 13 cards are equally likely. In an equivalent model, the cards are chosen and dealt one at a time. When choosing a card, the dealer is equally likely to pick any of the cards that remain in the deck.

**If you dealt 13 cards, what is the probability that the 13th card is a King?****

Now with the problem with the naive 1/13 is that it does not consider the possibilities that a king is drawn before, as each king drawn will alter the probability?Does it matter how much card we have dealt before? Monte Carlo simulation gives always lower chance then expected

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Probability_to_draw_a_king
{

    class Program
    {

        static void Main(string[] args)
        {
            for (int r = 0; r < 10; r++)
            {
                List<Tuple<int, int>> allCards = new List<Tuple<int, int>>();
                //initiliaze the desk 
                for (int i = 1; i <= 13; i++)
                {
                    for (int z = 1; z <= 4; z++)
                    {
                        Tuple<int, int> temp = new Tuple<int, int>(i, z);
                        allCards.Add(temp);

                    }
                }
                int successEvent = 0;
                Random rnd = new Random(Environment.TickCount);
                int iterations = 100000;//million
                List<Tuple<int, int>> copy = allCards.ToList();
                for (int i = 0; i < iterations; i++)
                {
                    copy = allCards.ToList();
                    for (int z = 1; z <= 12; z++)
                    {

                        bool found = false;
                        int card = rnd.Next(1, 14);
                        int color = rnd.Next(1, 5);
                        Tuple<int, int> tempCard = new Tuple<int, int>(card, color);

                        for (int q = 0; q < copy.Count; q++)
                        {
                            if (copy[q].Item1 == card && copy[q].Item2 == color)
                            {
                                found = true; break;
                            }
                        }
                        //if we havent found it we try one more time untill we find something to remove(z-1)
                        if (found == false)
                        {
                            z-=1;
                            continue;
                        }
                        //if we find the card we remove it
                        else
                        {

                            for (int q = 0; q < copy.Count; q++)
                            {
                                if (copy[q].Item1 == card && copy[q].Item2 == color)
                                {
                                    copy.Remove(allCards[q]);
                                }
                            }
                        }

                    }
                    // pick a card at random
                    int pickACard = rnd.Next(0, copy.Count);

                    if (copy[pickACard].Item1 == 8)
                    {
                        successEvent++;
                    }
                }
                Console.WriteLine("Expected number of occurences over " + iterations + " iterations" +
                    "is " + iterations * 1 / 13 + " . Actual number of occurences " + successEvent);
            }
        }


    }
}