Jump to content

Take a number, break it down to 5 diff. digits in a for loop to equal 100


slyte33

Recommended Posts

Here's what I'm wanting to do:

 

Number = 100

In a for loop take that number, 100, and break it down into 5 random numbers to equal 100.

 

So the for loop would output something like this:

 

1. 32

2. 5

3. 18

4. 9

5. 36

 

Total = 100

 

Everytime the numbers would be different.

 

I hope that explains it :)

 

Thanks!

 

Link to comment
Share on other sites

yowza. well, obviously the numbers can't be different EVERY time as the set of possibilities is limited.

 

uh. that's all I got. :-)

 

You're talking about the 5 numbers that equal 100 can't be different everytime?

Or are you talking about the actual number, 100?

 

Edit: Oh, I think I see what you mean. If so; I know not everytime, but how ever many times it can be different ofcourse  :P

Link to comment
Share on other sites

arguably the simplest way....

 

$total = 100;
$values = array();
for ($i = 1; $i <= 4; $i++) {
  $values[] = rand(1, 20);
}

$values[] = $total - array_sum($values);

echo "<pre>Values are:\n" . print_r($values, true) . "</pre>";

 

No excessive amount of looping involving a where loop or anything which could iterate thousands of times before it hits the correct numbers.  Only caveat being the last number will be very different....ranging from 20 to 96 versus 1 to 20 for the others.  A little more logic could solve that though....

Link to comment
Share on other sites

arguably the simplest way....

 

$total = 100;
$values = array();
for ($i = 1; $i <= 4; $i++) {
  $values[] = rand(1, 20);
}

$values[] = $total - array_sum($values);

echo "<pre>Values are:\n" . print_r($values, true) . "</pre>";

 

No excessive amount of looping involving a where loop or anything which could iterate thousands of times before it hits the correct numbers.  Only caveat being the last number will be very different....ranging from 20 to 96 versus 1 to 20 for the others.  A little more logic could solve that though....

 

Thank you.

I don't currently have my FTP open, but modifying your code, would this possibly work:

$total = 100;
$values = array();
for ($i = 1; $i <= $times; $i++) {
$n=100/10;
  $values[] = rand(1, $n);
}

$values[] = $total - array_sum($values);

for ($x=0;$x<=5;$x++)
{
echo "$x. ".$values[$x]."<br>";
}

 

Link to comment
Share on other sites

$times isn't defined and $n will always equal 10 (100 / 10 = 10), so the line that reads "$values[] = rand(1, $n)" may as well read "$values[] = rand(1, 10)".  Also the second for loop will execute 6 times, not 5.

 

Yeah $times is a $_POST['times'] data, just ignore that.

I've redone the code, and to my knowledge, the last number should not be the highest number always.

 

 

 

 

$total = 5000;
$values = array();
for ($i = 1; $i <= 5; $i++) {
$n=5000/2;
  $values[] = rand(1, $n);
}

$values[] = $total - array_sum($values);

for ($x=0;$x<=4;$x++)
{
echo "$x. ".$values[$x]."<br>";
}

 

 

Now the last number should not always be higher than the others.

This is just a prime example, though and I'm not sure if it will work.

And yes I forgot, the second loop should be a number less because of the [ 0 ] I think.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.