Return to Snippet

Revision: 24581
at March 4, 2010 09:50 by philokezzar


Initial Code
<?php
    $percentNS = 0.3;
    $percentSM = 0.5;
    $percentSH = 1.0 - $percentNS - $percentSM;
    
    $upperBoundNS = $percentNS;
    $upperBoundSM = $percentNS + $percentSM;
    $upperBoundSH = 1.0;
    
    for ($i = 0; $i < 20; $i++) {
        $rand = rand(0, 100);
        $rand = $rand / 100.0;
        
        if ($rand < $upperBoundNS) {
            echo 'not swing';
        }
        else if ($rand < $upperBoundSM) {
            echo 'swing and miss';
        }
        else {
            // $rand < $upperBoundSH
            echo 'swing and hit';
        }
        
        echo '<br />';
    }
?>

Initial URL
http://www.kirupa.com/forum/showthread.php?t=344243

Initial Description
Here is an example where the likelihood of NS is 30%, SM is 50%, and SH is 20%. I make 20 pitches and output what the batter does each time.

Initial Title
Random with Percentage Generator

Initial Tags
php

Initial Language
PHP