how to code php script that can generate random number array . For example, there are 25 numbers from 1 to 25 . Each time, the script can randomly generate five number out of the 25 numbers . The five numbers never repeat, I call them one number array . If the sum of the five number is between 35 and 60, then print the number array. If not, ignore it .
<?php $numbers = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25); shuffle($numbers); $number1 = $numbers[0]; $number2 = $numbers[1]; $number3 = $numbers[2]; $number4 = $numbers[3]; $number5 = $numbers[4]; $total = $number1 + $number2 + $number3 + $number4 + $number5; if($total >= 35 and $total <= 60){ echo $total; } ?> PHP: Are you looking for something like that? You can simplify it a little more, but I wrote it all out so you can see what the script was doing. If you want the simplifier version you can use this <?php $numbers = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25); shuffle($numbers); $total = $numbers[0]; + $numbers[1]; + $numbers[2]; + $numbers[3]; + $numbers[4]; if($total >= 35 and $total <= 60){ echo $total; } ?> PHP: When you use the shuffle function it will randomly shuffle everything in the array. Then from there you simply call the first 5 numbers from the array (or how ever many numbers you want to call).
bigrollerdave I appreciate your feedback . how to echo the number array rather than echo the $total . lol Also I know little about php . Does the code meet the requirement "the five number never repeat" ?
You want to echo the 5 total numbers? Well it's impossible to never have a number repeat when using a set amount of numbers. If I gave you 25 dice and you rolled them 1 million times the chance of you rolling the same exact thing twice will be really slim. Although it is possible to have the same numbers twice. Using the shuffle though will shuffle up the array every time someone visits the page though so it will almost never repeat for the same user, if that makes sense.
<?php $numbers = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25); shuffle($numbers); $total = $numbers[0] + $numbers[1] + $numbers[2] + $numbers[3] + $numbers[4]; if($total >= 35 and $total <= 60){ echo $numbers[0] .' '. $numbers[1] .' '. $numbers[2] .' '. $numbers[3] .' '. $numbers[4]; } ?> PHP: There you go that will not display all 5 numbers in the array if the total is above or equal to 35 and less then or equal to 60
@bigrollerdave: The range() function could save you some time if you find you need to generate a list of numbers again.
Here is some cleaner code, it's untested, but should work. $numbers = range(1, 25); shuffle($numbers); $new_nums = array_slice($numbers, 0, 5); $total = array_sum($new_nums); if($total >= 35 && $total <= 60) { echo implode('-', $new_nums); } PHP:
Yeah, I like to display code simply for those who don't have a strong grasp of php. I like to make the code understandable so they will not only be able to use it, but understand it. The range function makes it a little simpler but it's harder to get a grasp of.
Not really an excuse. If you put something they don't know, they should goto http://php.net/range <- They have examples and show basics.