Ballot Simulator - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of Ballot Simulator.php

  1. <?php 
  2.         $blue = 225000;
  3.         $red = 5000;
  4.  
  5.         $size_of_bags = 100;
  6.  
  7.         $blue_bags = 0;
  8.         $red_bags = 0;
  9.         $purple_bags = 0;
  10.  
  11.         $current_consecutive_blues = 0;
  12.         $max_consecutive_blues = 0;
  13.         $total_consecutive_blue_streaks = 0;
  14.  
  15.         // While we still have balls to put in bags.
  16.         while ($blue > 0 || $red > 0) {
  17.             // The current bag.
  18.             $bag = ["blue" => 0, "red" => 0];
  19.             // Loop through until you've filled up a bag.
  20.             for ($i = 1; $i <= $size_of_bags; $i++) {
  21.                 // Do we still have at least 1 red or blue ball?
  22.                 if ($blue + $red != 0) {
  23.                     $odds_of_blue = round(100 * ($blue / ($blue + $red)));
  24.                 } else {
  25.                 // We don't so we're done.
  26.                     break;
  27.                 }
  28.                 // Pick a random number between 1 and 100.
  29.                 $rand = rand(1, 100);
  30.                 // Did we pick blue? Note if we're out of blue, then odds are 0. Out of red then odds are 100.
  31.                 if ($rand <= $odds_of_blue ){
  32.                     // Pull a blue ball out of the pool and add it to the bag.
  33.                     $blue--;
  34.                     $bag['blue']++;
  35.                 // We didn't pick blue, do we have any reds left to place?
  36.                 } elseif ($red > 0){
  37.                     // Put in a red ball.
  38.                     $red--;
  39.                     $bag['red']++;
  40.                 } else {
  41.                     // We're out of balls, do nothing.
  42.                     break;
  43.                 }
  44.             }
  45.             // We've filled up a bag, let's examine it.
  46.  
  47.             // Is it a bag of all blues?
  48.             if ($bag['red'] == 0){
  49.                 // Add to the blue streak.
  50.                 $current_consecutive_blues++;
  51.                 // Add to our count of all blue bags.
  52.                 $blue_bags++;
  53.             // Otherwise it's an all red bag or a purple bag.
  54.             } else {
  55.                 // Have we had a streak of blue bags? A streak is more than 1 blue bag.
  56.                 if ($current_consecutive_blues > 1){
  57.                     // We have a new blue streak. Then add to the streak count.
  58.                     $total_consecutive_blue_streaks++;
  59.                     // Was this streak higher than our max streak?
  60.                     if ($current_consecutive_blues > $max_consecutive_blues){
  61.                         // It was so set the max streak count to this streak.
  62.                         $max_consecutive_blues = $current_consecutive_blues;
  63.                     }
  64.                 }
  65.                 // Reset our blue streak.
  66.                 $current_consecutive_blues = 0;
  67.  
  68.                 // Is it a red or purple bag?
  69.                 if ($bag['blue'] == 0){
  70.                     // All red bag. Count it.
  71.                     $red_bags++;
  72.                 } else {
  73.                     // Then it's a purple bag.
  74.                     $purple_bags++;
  75.                 }
  76.             }
  77.         }
  78.  
  79.         $data = ["BLUE BAGS" => $blue_bags, "RED BAGS"=>$red_bags, "PURPLE BAGS"=>$purple_bags,
  80.                  "TOTAL BLUE STREAKS"=>$total_consecutive_blue_streaks, "MAX BLUE STREAK"=>$max_consecutive_blues] ;
  81.  
  82.         foreach ($data as $key=>$value){
  83.             echo $key . ": " . $value . "\n";
  84.         }
File Description
  • Ballot Simulator
  • PHP Code
  • 26 Aug-2023
  • 3.25 Kb
You can Share it: