3v4l.org

run code in 300+ PHP versions simultaneously
<?php $runs = 1000; $agree = array('count' => 0, 'To Mom' => 0, 'To Dad' => 0, 'Joint' => 0); $disagree = array('count' => 0, 'Sole Fight' => 0, 'FsMj' => 0, 'MsFj' => 0, 'MfFm' => 0, 'other' => 0); $dadSole = 0; $momSole = 0; $fChk = array('f' => 0, 'm' => 0, 'j' => 0); $mChk = array('f' => 0, 'm' => 0, 'j' => 0, 'o' => 0); for ($i=0; $i < $runs ; $i++) { $mPref = rand(1,100); $fPref = rand(1,100); #### checking random chooser #### switch(fChoice($fPref)){ case 'f': $fChk['f']++; break; case 'm': $fChk['m']++; break; case 'joint': $fChk['j']++; break; } switch(mChoice($mPref)){ case 'f': $mChk['f']++; break; case 'm': $mChk['m']++; break; case 'joint': $mChk['j']++; break; case 'other': $mChk['o']++; break; } #### ####### ####### ####### #### if(fChoice($fPref) == mChoice($mPref) ) { // if in agreement $agree['count']++; // increment counter switch (fChoice($fPref)) { // increment specific choice agreed upon case 'f': $agree['To Mom']++; break; case 'm': $agree['To Dad']++; break; case 'joint': $agree['Joint']++; break; } } else { // calculate stats for disagreements if(mChoice($mPref) == 'other') { continue; } // toss out Dad wants other cases $disagree['count']++; if(fChoice($fPref) == 'f') { // mom wants sole $momSole++; } if(mChoice($mPref) == 'm'){ // dad wants sole $dadSole++; } if(fChoice($fPref) == 'f' && mChoice($mPref) == 'm') { // both want sole $disagree['Sole Fight']++; } elseif(fChoice($fPref) == 'f' && mChoice($mPref) == 'joint') { // Mom Sole - Dad Joint $disagree['FsMj']++; } elseif(fChoice($fPref) == 'joint' && mChoice($mPref) == 'm') { // Mom Joint - Dad Sole $disagree['MsFj']++; } elseif(fChoice($fPref) == 'm' && mChoice($mPref) == 'f') { // hot potato $disagree['MfFm']++; } else { $disagree['other']++; // echo 'Dad wants '.mChoice($mPref).' and Mom wants '.fChoice($fPref).'<br>'; } } } $sigDigits = 2; $percentAgree = $agree['count'] / $runs * 100; $percentDisagree = $disagree['count'] / $runs * 100; $agreeMom = round($agree['To Mom'] / $agree['count'] * 100, $sigDigits); $agreeDad = round($agree['To Dad'] / $agree['count'] * 100, $sigDigits); $agreeJnt = round($agree['Joint'] / $agree['count'] * 100, $sigDigits); $disagreeSole = round($disagree['Sole Fight'] / $disagree['count'] * 100, $sigDigits); $disagreeFsMj = round($disagree['FsMj'] / $disagree['count'] * 100, $sigDigits); $disagreeMsFj = round($disagree['MsFj'] / $disagree['count'] * 100, $sigDigits); $disagreeNone = round($disagree['MfFm'] / $disagree['count'] * 100, $sigDigits); $disagreeOther = round($disagree['other'] / $disagree['count'] * 100, $sigDigits); $dadPercent = round($dadSole / $disagree['count'] * 100, $sigDigits); $momPercent = round($momSole / $disagree['count'] * 100, $sigDigits); // 'Sole Fight' => 0, 'FsMj' => 0, 'MsFj' => 0, 'other' => 0 echo "From {$runs} runs there were {$percentAgree}% agreements and {$percentDisagree}% disagreements."; echo "From the agreements <ul> <li>{$agreeMom}% go to Mom</li> <li>{$agreeDad}% go to Dad</li> <li>{$agreeJnt}% joint custody</li> </ul>"; echo "From the disagreements <ul> <li> {$momPercent}% of Mom's want sole custody</li> <li> {$dadPercent}% of Dad's want sole custody</li> <br><br> <li>{$disagreeSole}% both want Sole Custody</li> <li>{$disagreeFsMj}% of cases: Mom wants Sole; Dad joint</li> <li>{$disagreeMsFj}% of cases: Dad wants Sole; Mom joint</li> <li>{$disagreeNone}% of cases: Hot Potato the Kids!</li> <li>{$disagreeOther}% are in another configuration of disagreement</li> </ul>"; foreach ($fChk as &$optionCount) { $optionCount = round($optionCount / $runs * 100, $sigDigits); } foreach ($mChk as &$optionCount) { $optionCount = round($optionCount / $runs * 100, $sigDigits); } echo "<br><br>"; print_r($fChk); echo "<br><br>"; print_r($mChk); function fChoice ($num) { if($num <= 82) { return 'f'; } if($num <= 85) { return 'm'; } else { return 'joint'; } } function mChoice ($num) { if($num <= 29) { return 'f'; } if($num <= 62) { return 'm'; } if ($num <= 97) { return 'joint'; } else { return 'other'; } }

preferences:
34.56 ms | 402 KiB | 5 Q