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 Link to comment https://forums.phpfreaks.com/topic/91079-random/ 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); ?> Link to comment https://forums.phpfreaks.com/topic/91079-random/#findComment-466805 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); ?> Link to comment https://forums.phpfreaks.com/topic/91079-random/#findComment-466812 Share on other sites More sharing options...
dc_jt Posted February 14, 2008 Author Share Posted February 14, 2008 Thanks that works great Link to comment https://forums.phpfreaks.com/topic/91079-random/#findComment-466818 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.