This is my code below. The code itself echoes if the sum of three of random numbers equals 100. For now to get a result of 100 as $valuestotal I have to reload the page a lot. But I want to get the result of 100 as $valuestotal every time I reload the page. How can I do this?
I thought it would be possible with a loop in loop. But it gives the same result x times. I want PHP to keep trying different random values, and when it reaches to the value of 100 as $valuestotal, it should echo the $valuestotal...
What I really want is, for example, I want three random numbers to get total of 100, example is 35 and 25 and 50. Or 25 and 15 and 60... the list goes on...
How is this possible?
$values = rand(1, 100);
$values2 = rand(1, 100);
$values3 = rand(1, 100);
$valuestotal = $values + $values2 + $values3;
//
while ($valuestotal === 100)
{
echo $values;
echo '<br>';
echo $values2;
echo '<br>';
echo $values3;
echo "<br>";
echo $valuestotal;
}
One more possible approach. The code is slightly more complex than some of the other answers, but I wanted to add one way that would only need to loop once.
$target = 100;
$n = 3;
while ($n) {
if (1 < $n--) {
$addend = rand(0, $target - ($n - 1));
$target -= $addend;
$addends[] = $addend;
} else {
$addends[] = $target;
}
}
var_dump($addends);
Basically you subtract a random number between 0 and the remainder of the previous subtraction n times until there's only one repetition left, and the remainder is the last piece. $n - 1 is there so that it doesn't randomly subtract too much for there to be enough left for the rest of the repetitions.
You will not need to loop inside of a loop. For example, you could do this:
<?php
$keepChecking = true;
$values = 0;
$values2 = 0;
$values3 = 0;
$valuestotal = 0;
while($keepChecking){
$values = rand(1, 100);
$values2 = rand(1, 100);
$values3 = rand(1, 100);
$valuestotal = $values + $values2 + $values3;
if ($valuestotal === 100){
$keepChecking = false;
}
}
echo $values;
echo '<br>';
echo $values2;
echo '<br>';
echo $values3;
echo "<br>";
echo $valuestotal;
?>
Results may be:
2
37
61
100
Can test here: http://phpfiddle.org/lite
In a while loop, the block will be execute as long as the condition is
true. So you can think, what do you have to execute multiple times, and
when should it stop?
What you need to execute multiple times is the gathering of random numbers:
while ( ... ) {
# roll dice, save numbers
}
Now this should stop when sum is 100. The condition stated in the loop must be true for it to run multiple times, so in this sense, it has to run while the sum is not 100. Here is a key point that you got reversed. If your condition is false (by chance it will likely be after first run) then the loop already stopped. You have to run while the sum is not 100 so that when it is, it won't gather random numbers anymore. Then you proceed with echoing them.
$valuestotal = 0;
while ($valuestotal != 100) {
$value1 = rand(1, 100);
$value2 = rand(1, 100);
$value3 = rand(1, 100);
$valuestotal = $value1 + $value2 + $value3;
}
echo "$value1<br>$value2<br>$value3<br>$valuestotal";
As an alternative syntax, you could store the values in an array and use
the array_sum() function:
$numbers = [];
while (array_sum($numbers) != 100)
$numbers = [
rand(1, 100),
rand(1, 100),
rand(1, 100),
];
echo join(' + ', $numbers), ' = ', array_sum($numbers);