PHPTOM Posted August 2, 2008 Share Posted August 2, 2008 Hi, Can someone point me in the right direction. This is what I need to do: I need to choose 12 random numbers from 1 to 80 but they cant be the same. If I do rand(0,80) for say 12 variables there is a chance that it could be the same as one of the others. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/117850-solved-random-number-only-once/ Share on other sites More sharing options...
LemonInflux Posted August 2, 2008 Share Posted August 2, 2008 $num = array(); for($i=0;$i>12;$i++) { $num[] = chooseRand(); } function chooseRand() { $n = rand(0,80); if(in_array($n, $num) { chooseRand(); } else { return $n; } } ---------------- Now playing: Linkin Park & Jay-Z - Dirt Off Your Shoulder / Lying From You via FoxyTunes Quote Link to comment https://forums.phpfreaks.com/topic/117850-solved-random-number-only-once/#findComment-606153 Share on other sites More sharing options...
kenrbnsn Posted August 2, 2008 Share Posted August 2, 2008 A much easier way: <?php $nums = range(1,80); shuffle($nums); $nums12 = array_slice($nums,0,12); echo implode(', ',$nums12); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/117850-solved-random-number-only-once/#findComment-606162 Share on other sites More sharing options...
LemonInflux Posted August 2, 2008 Share Posted August 2, 2008 Oh yeah, haha. I don't think like that ---------------- Now playing: Linkin Park & Jay-Z - Numb/Encore via FoxyTunes Quote Link to comment https://forums.phpfreaks.com/topic/117850-solved-random-number-only-once/#findComment-606165 Share on other sites More sharing options...
PHPTOM Posted August 2, 2008 Author Share Posted August 2, 2008 Thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/117850-solved-random-number-only-once/#findComment-606182 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.