Reputation: 11
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;
}
Upvotes: 0
Views: 1909
Reputation: 1
$arr = array();
$count = 53;
$total = 500;
for ($i = 0; $i < $count; $i++)
{
$arr[] = 0;
}
for ($j = 0; $j < $total; $j++)
{
$arr[rand() % $count]++;
}
echo "<pre>";
print_r($arr);
echo "</pre>";
Upvotes: 0
Reputation: 41810
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.
Upvotes: 4
Reputation: 16132
Randomise the values inside a loop and each time set a new value to $valuestotal
the loop will only stop if $valuestotal = 100
.
$valuestotal = 0;
//
while ($valuestotal !== 100) {
echo $values;
echo '<br>';
echo $values2;
echo '<br>';
echo $values3;
echo "<br>";
$values = rand(1, 100);
$values2 = rand(1, 100);
$values3 = rand(1, 100);
echo $valuestotal . '<br>';
$valuestotal = $values + $values2 + $values3;
}
Upvotes: 0
Reputation: 59297
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);
Upvotes: 3
Reputation: 1057
You will need to get only 2 random numbers which sum up to less than 100, than subtract that sum from 100 to get the 3rd random random. This function does just that in a simplistic manner:
function get_three_rand_numbs_sum_hundred(){
$num1 = rand(1, 99); // Get first random number.
$num2 = rand(1, 99); // Get second random number.
$num3 = 0; // Declare variable which will evnetually hold 3rd random number.
/* While the sum of the first 2 random numbers above is more than a 100,
get another random number for Number 2.*/
while(($num1 + $num2) >= 100){
$num2 = rand(1, 99);
}
// Get the 3rd random number by subtracting the sum of the first two from 100.
$num3 = 100 - ($num1 + $num2);
echo "Number 1 : ".$num1."\n";
echo "Number 2 : ".$num2."\n";
echo "Number 3 : ".$num3."\n";
echo "Sum : ".($num3+$num2+$num1)."\n";
}
NOTE: The rand() function treats the max parameter provided to it as an exclusive value, so you should use 99 instead of a 100 since you are certain you do not want 100 from only 1 variable.
Upvotes: 1
Reputation: 876
<?php
$valuesTotal = 0;
while ($valuesTotal != 100){
$value1 = rand(1, 100);
$value2 = rand(1, 100);
$value3 = rand(1, 100);
$valuesTotal = $value1+$value2+$value3;
echo "\$value1 = $value1 :: \$value2 = $value2 :: \$value3 = $value3 :: \$valuesTotal = $valuesTotal";
}
?>
Another way :
<?php
while (true){
$value1 = rand(1, 100);
$value2 = rand(1, 100);
$value3 = rand(1, 100);
$valuesTotal = $value1+$value2+$value3;
echo "\$value1 = $value1 :: \$value2 = $value2 :: \$value3 = $value3 :: \$valuesTotal = $valuesTotal";
if($valuesTotal == 100){
break;
}
}
?>
This should give you an Idea, it may take a fraction of second to complete the script, or it could take forever, depending on your luck!
You need to write the code to be repeatedly executed between the brackets of your loop, not on top of it.
Upvotes: 1
Reputation: 339
I am assuming that you need to get 3 different random values whose sum is 100
Try This
$total = 100;
$val1 = rand(1,$total);
$val2 = rand(1, ($total-$val1));
$val3 = $total - ($val1 + $val2);
print($val1."\n");
print($val2."\n");
print($val3."\n");
Upvotes: -1
Reputation: 30893
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
Upvotes: 2