Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
<? header("Content-type: text/plain"); // Establishes the pool of cards $cardPool = array('Spade', 'Heart', 'Diamond', 'Club'); // Sorts them alphabetically sort($cardPool); // Displays cardpool print_r($cardPool); // We want to count each try to get 10 hearts and first we set the counter to zero $tryCounter = 0; do { // Add 1 to the number of tries we've done $tryCounter++; // Establish and reset the "hand" $hand = array(); // Tell our program to do something 10 times for($run = 1; $run <= 10; $run++) { // Randomly pick one card from the cardpool $pickedCard = $cardPool[array_rand($cardPool)]; // Accumulate the number of "picked card" in the hand by one $hand[$pickedCard]++; } //printf("Hearts after run %u: %u\n", $run, $cardSet['Heart']); // We have this to avoid making the program run infinitely if we screw up the code if( $tryCounter > 10000000 ) { break; } // At end of try, see if we have 10 Hearts in hand, if so, stop loop } while( $hand['Heart'] < 10 ); // Display a report on how many runs it took to gain a hand of only hearts printf("Result reached after %u runs\n", $tryCounter); // Print the full hand print_r($hand);
This paste will be private.
From the Design Piracy series on my blog: