title: Exactly one high card in a 6 card hand using simulations
body: <p>So I've got a probability problem that (out of pure boredom) I decided to try and solve using simulations.
  Problem: What is the probability of drawing exactly one high card in a 6 card hand.</p>
  
  <p>Now, this problem is specifically about some game that's played in my country so for some weird reason there are 21 high cards, but that's not important.</p>
  
  <p>Solving this problem by hand, using basic combinatorics I got:
  <a href="https://i.stack.imgur.com/AfK6L.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AfK6L.gif" alt="enter image description here"></a></p>
  
  <p>And now, here's the way I simulated it in C:</p>
  
  <p>The main function:</p>
  
  <pre><code>int main(void)
  {
  
      srand(time(0));
      int deck[52];
  
      int i;
      for(i = 0; i&lt;21; i++) deck[i] = 1;
      for(i = 21; i&lt;52; i++) deck[i] = 0;
  
      int n;
      printf("# of simulations: ");
      scanf("%d", &amp;n);
  
      int memo[52];
  
      int hits = 0;
      for(i = 0; i&lt;n; i++){
        clear_memo(memo);
        hits += simulate(deck, memo);
      }
      printf("Result: %lf\n", (double)hits/n);
  
  
  }
  </code></pre>
  
  <p>So the <em>deck</em> is an array of 52 numbers where the first 21 have the value 1 (high cards) and the other 31 elements have the value 0 (low cards).</p>
  
  <p>The <em>memo</em> will be sent to the simulation function each time to keep track of which cards have already been drawn. The memo also gets reset every time using the <em>clear_memo</em> function which does nothing but set all the values to zero.</p>
  
  <p>Then it calls the simulation functions and counts the hits.</p>
  
  <p>Here's the simulation function:</p>
  
  <pre><code>int simulate(int * deck, int * memo){
  
    //I draw the first card separetly in order to initialize the had_high variable
    int index = ( rand() % 52 );
    int card = deck[index];
    int had_high = (card == 1);
    memo[index] = 1;
  
    //printf("%d ", index);
  
    int i = 1;
    while(i &lt; 6){
  
      int draw = (rand() % 52);
      //printf("%d ", draw);
      if(memo[draw]) continue;
  
      index = draw;
      card = deck[index];
      memo[index] = 1;
  
      if(card){
          if(had_high) { //meaning there are 2 high cards, no hit
            //printf("\n");
            return 0;
          }
          had_high = 1; //if not, then this is the first high card
      }
  
      i++;
    }
    printf("\n");
    return had_high; //the function would've reached this point even if all the cards had been low
                     //therefore I return had_high instead of just 1
  
  }
  </code></pre>
  
  <p>The simulation function itself works, I've tested it separately a lot of times and there seem to be no problems with it.</p>
  
  <p>However, when I run the program with a high number of simulations (100k or 1m) the result is always approx. 0.175 which is not what I got with my by hand calculation.</p>
  
  <p>I am reasonably certain that my by hand calculation is correct (but correct me if I'm wrong there as well).</p>
  
  <p>If I'm right about the by hand calculations then there must be something wrong with how I simulated this event. One of my thoughts was that it had something to do with the <em>rand</em> function and how it's pseudo-random, but I really don't know as it is very hard to test anything that works with random numbers.</p>
  
  <p>Any ideas?</p>
  
  <p>Thanks.</p>
  
  <p>EDIT: 
  As per request of klutt:</p>
  
  <pre><code>void clear_memo(int * memo){
    int i = 0;
    for(;i&lt;52;i++) memo[i] = 0;
  }
  </code></pre>