d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help In Php
Add Reply New Topic New Poll
Member
Posts: 868
Joined: Jun 10 2010
Gold: 96.51
Feb 2 2013 09:07am
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
Member
Posts: 14,109
Joined: Nov 6 2005
Gold: 1,939.00
Feb 3 2013 11:38am
Here you go. Result is 0.065 as you wished.

Code

<?php
//chance (rand(1,10)+rand(1,100) >=100)

//Idea: add all desired results and devide them by all possible results

$result=false;
$a=1; //first random number
$b=1; //second random number
$d=0; //number of desired results
$p=0; //number of all possibilities

while($a<=10){
$b=1;//set back $b to 1 after every loop
while($b<=100){
 $c=$a+$b;
 $result=check($c);
  if($result){//check if the result was over 100
   $d++;//add one desired result
  }
 $p++;
 $b++;
}
$a++;
}

function check($random){
if($random>=100){
return true;
}
}

$chance=$d/$p;
echo $chance;

?>
Member
Posts: 14,109
Joined: Nov 6 2005
Gold: 1,939.00
Feb 3 2013 11:48am
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.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll