slyte33 Posted October 18, 2010 Share Posted October 18, 2010 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! Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted October 19, 2010 Share Posted October 19, 2010 yowza. well, obviously the numbers can't be different EVERY time as the set of possibilities is limited. uh. that's all I got. :-) Quote Link to comment Share on other sites More sharing options...
slyte33 Posted October 19, 2010 Author Share Posted October 19, 2010 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 Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted October 19, 2010 Share Posted October 19, 2010 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.... Quote Link to comment Share on other sites More sharing options...
slyte33 Posted October 19, 2010 Author Share Posted October 19, 2010 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>"; } Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted October 19, 2010 Share Posted October 19, 2010 $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. Quote Link to comment Share on other sites More sharing options...
slyte33 Posted October 19, 2010 Author Share Posted October 19, 2010 $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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.