fork download
  1. <?php
  2.  
  3. $deck = array_merge(array_fill(0, 26, 'R'), array_fill(0, 26, 'B'));
  4. $games = 0;
  5. $wins = 0;
  6.  
  7. for ($games = 1; $games < 100 * 1000; $games++) {
  8. $cur_deck = $deck;
  9. shuffle($cur_deck);
  10.  
  11. $reds = 26;
  12. $blacks = 26;
  13. $deck_size = $reds + $blacks;
  14.  
  15. while ($deck_size >= 2) {
  16. // Betting cases
  17. if ($reds > $blacks) { break; } // If more red then black, bet
  18. if ($reds == 1) { break; } // If only one red left, bet
  19. if ($deck_size == 1) { break; } // If only one card left, bet
  20.  
  21. $flip = array_pop($cur_deck);
  22. if ($flip == 'R') { $reds--; }
  23. if ($flip == 'B') { $blacks--; }
  24. $deck_size--;
  25. }
  26.  
  27. // If the next flip is red, we win.
  28. $flip = array_pop($cur_deck);
  29. if ($flip == 'R') { $wins++; }
  30. }
  31.  
  32. echo "Total Games: " . number_format($games) . PHP_EOL;
  33. echo "Total Wins: " . number_format($wins) . PHP_EOL;
  34. echo "Win Percent: " . number_format(100 * ($wins / $games), 2) . PHP_EOL;
  35.  
  36. ?>
Success #stdin #stdout 1.47s 20568KB
stdin
Standard input is empty
stdout
Total Games: 100,000
Total Wins:  49,985
Win Percent: 49.99