Advertisement
Guest User

Untitled

a guest
Nov 29th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.44 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <time.h>
  5.  
  6. #define SCENARIO_COUNT 1000
  7.  
  8. bool fiftyFifty();
  9.  
  10. int main()
  11. {
  12.     srand(time(NULL));
  13.     bool firstFlip, secondFlip;
  14.     int scenariosRun, hhCount=0, htCount=0, ttCount=0;
  15.  
  16.     for(scenariosRun = 0; scenariosRun < SCENARIO_COUNT; scenariosRun++)
  17.     {
  18.         firstFlip = fiftyFifty(); //both flips are 50/50
  19.         secondFlip= fiftyFifty();
  20.        
  21.         if(!firstFlip && !secondFlip)
  22.         {
  23.             ttCount++;
  24.         }
  25.        
  26.         while(!firstFlip && !secondFlip) //check for TT
  27.         {
  28.             firstFlip = fiftyFifty(); //flip both coins again as long as both are TT
  29.             secondFlip= fiftyFifty();
  30.         }
  31.        
  32.         if(firstFlip && secondFlip) //if both coins are heads, this counter goes up
  33.         {
  34.             hhCount++;
  35.         }
  36.         if(!firstFlip || !secondFlip) //if either coin is tails, this counter goes up
  37.         {
  38.             htCount++;
  39.         }
  40.     }
  41.  
  42.     //print results
  43.     printf("Results gathered from %d scenarios in which one heads is guaranteed:", scenariosRun);
  44.     printf("\nTimes both were heads:  %d", hhCount);
  45.     printf("\nTimes one was heads:  %d", htCount);
  46.     printf("\nTT reflips:  %d", ttCount);
  47.    
  48.     return 0;
  49. }
  50.  
  51. bool fiftyFifty()
  52. {
  53.     int isCrit = rand() % 2;
  54.     if (isCrit == 1)
  55.     {
  56.         return true;
  57.     }
  58.     else
  59.     {
  60.         return false;
  61.     }
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement