brown2005 Posted January 15, 2008 Share Posted January 15, 2008 Hi, i want to have say 3 numbers $number1 = RAND(1,100); $number2 = RAND(1,100); $number3 = RAND(1,100); but none of the numbers can be the same and then i want to sort them in order of smallest frist to largest.. any help would be much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/86156-random-number-script/ Share on other sites More sharing options...
tinker Posted January 15, 2008 Share Posted January 15, 2008 Instead of separate variables use an array, then use a loop to generate, checking if in_array, if so do again. Then when finished just sort the array! Quote Link to comment https://forums.phpfreaks.com/topic/86156-random-number-script/#findComment-440014 Share on other sites More sharing options...
trq Posted January 15, 2008 Share Posted January 15, 2008 <?php function rands($n,$s,$e) { $return = array(); for ($i=0;$i<$n;$i++) { $num = rand($s,$e); do { $num = rand($s,$e); } while (in_array($num,$return)); $return[] = $num; } return $return; } $rands = rands(3,1,100); foreach ($rands as $number) { echo $number . "\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/86156-random-number-script/#findComment-440019 Share on other sites More sharing options...
brown2005 Posted January 15, 2008 Author Share Posted January 15, 2008 thanks very much thorpe. one thing.. so where would i add the sort thing into this code u gave me, please Quote Link to comment https://forums.phpfreaks.com/topic/86156-random-number-script/#findComment-440032 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.