Quote (Axelomahawk @ 2 Feb 2013 16:07)
I need to calculate the chance that
rand(1,10)+rand(1,100) >=100
The answer is 6.5 and im really close with this script, something is wrong with it but i dont know what.
<?php
$alt=1;
$arg=1;
$current=0;
while($arg >= 10) {
$alt++;
if(($alt)+($arg) >= 100){
$current++;
}
if($alt = 100) {
$alt = 1;
$arg++;
}
}
echo $current;
echo '<br>'.$arg;
?>
I have also done
while($count < 10000) {
$arg = rand(1.0, 10.0);
$arg2 = rand(1.0,100.0);
$mart = $arg+$arg2;
if($mart >= 100) {
$count++;
}
else {
$nocount++;
}
}
echo $count.'<br>'.$nocount;
$ugare = ($count/($count+$nocount))*100;
echo '<br>'.$ugare.'<br>';
echo '<br>'.substr($ugare, 0, 4);
?>
That works, by testing in cycles. But if i want an exact answer, wouldn't the top one be correct? If anyone could just point out the issue, because im sure its not anything major...
Thanks peace
To point out some of your mistakes:
Code
if($alt = 100) {
= means assignment and has nothing to do with comparison. You have to use == operator in this case
Then it is just about math. There is probably a formula you could use, I just decided to count all possibilities and make a simple division since the problem isn't that hard. It really seems you have to work on basics.