The following code generates two random decimal values, then subtracts them to get $c
.
$a = mt_rand(5, 75);
$b = mt_rand(5, 75);
$adjuster = mt_rand(2, 20);
do {
$decimal_selector = mt_rand(1, 6);
if ($decimal_selector == 1) {
$a = $a / 10;
$b = $b / 10;
}
if ($decimal_selector == 2) {
$a = $a / 10;
$b = $b / 100;
}
if ($decimal_selector == 3) {
$a = $a / 100;
$b = $b / 10;
}
if ($decimal_selector == 4) {
$a = $a / 100;
$b = $b / 100;
}
if ($decimal_selector == 5) {
$a = $a / 1000;
$b = $b / 1000;
}
if ($decimal_selector == 6) {
$a = $a / 1000;
$b = $b / 100;
}
if($b < $a)
$b = $b + ($a - $b) + $adjuster;
$c = $b - $a;
} while((is_int($a) == true && is_int($b) == true) || is_int($c) == true);
The do-while loop is trying to ensure that $a
and $b
are both not integers, and also that $c
is not an integer. But I keep getting times where $c
actually is an integer.
If I use gettype
it keeps saying $c
is a "double". Why, when $c
ends up being something like 7
?
EDIT: I keep getting random infinite loops from this code below. Any ideas why?
do{
$a = mt_rand(5, 75);
$b = mt_rand(5, 75);
$adjuster = mt_rand(2, 20);
$decimal_selector = mt_rand(1, 6);
if ($decimal_selector == 1){
$a = $a / 10;
$b = $b / 10;
}
if ($decimal_selector == 2){
$a = $a / 10;
$b = $b / 100;
}
if ($decimal_selector == 3){
$a = $a / 100;
$b = $b / 10;
}
if ($decimal_selector == 4){
$a = $a / 100;
$b = $b / 100;
}
if ($decimal_selector == 5){
$a = $a / 1000;
$b = $b / 1000;
}
if ($decimal_selector == 6){
$a = $a / 1000;
$b = $b / 100;
}
if($b < $a){
$b = $b + ($a - $b) + $adjuster;
}
$c = $b - $a;
if(intval($c) == $c) {
$c_is_int = 1;
} else {
$c_is_int = 0;
}
echo intval($c) . '<br>';
echo $c_is_int . '<br>';
echo $c . '<br><br>';
} while($c_is_int == 1);