dc_jt Posted February 14, 2008 Share Posted February 14, 2008 Hi Ive got numbers 1 - 6. I want to echo out 3 of these numbers at random but the same number cant appear twice. E.g 1,3,5 would be correct 1,4,5 would be incorrect. How would I do this? Thanks Quote Link to comment Share on other sites More sharing options...
trq Posted February 14, 2008 Share Posted February 14, 2008 <?php $n = array(); for ($i = 0; $i <=3; $i++) { $tmp = rand(1,6); if (!in_array($tmp,$n)) { $n[] = $tmp; } } echo implode(' ', $n); ?> Quote Link to comment Share on other sites More sharing options...
trq Posted February 14, 2008 Share Posted February 14, 2008 Sorry, thats way off. Half asleep here. <?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,6); echo implode(' ', $rands); ?> Quote Link to comment Share on other sites More sharing options...
dc_jt Posted February 14, 2008 Author Share Posted February 14, 2008 Thanks that works great 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.