iandavies Posted April 13, 2006 Share Posted April 13, 2006 Hi,i am a bit clueless with php. i need to generate 3 unqiue random numbers between 1 and 6. I can use rand(1,6) to get the numbers but not sure how to check if they unique or not. can someone help me please!thanks Quote Link to comment Share on other sites More sharing options...
predator12341 Posted April 13, 2006 Share Posted April 13, 2006 [!--quoteo(post=364380:date=Apr 13 2006, 01:59 PM:name=iandavies)--][div class=\'quotetop\']QUOTE(iandavies @ Apr 13 2006, 01:59 PM) [snapback]364380[/snapback][/div][div class=\'quotemain\'][!--quotec--]Hi,i am a bit clueless with php. i need to generate 3 unqiue random numbers between 1 and 6. I can use rand(1,6) to get the numbers but not sure how to check if they unique or not. can someone help me please!thanks[/quote]ok man bare wid us for a min or 2 and i will write the code for ya kk (well something along the lines you want anyway) Quote Link to comment Share on other sites More sharing options...
iandavies Posted April 13, 2006 Author Share Posted April 13, 2006 [!--quoteo(post=364397:date=Apr 13 2006, 02:26 PM:name=predator12341)--][div class=\'quotetop\']QUOTE(predator12341 @ Apr 13 2006, 02:26 PM) [snapback]364397[/snapback][/div][div class=\'quotemain\'][!--quotec--]ok man bare wid us for a min or 2 and i will write the code for ya kk (well something along the lines you want anyway)[/quote]hi, that would be a bonus, thaks Quote Link to comment Share on other sites More sharing options...
predator12341 Posted April 13, 2006 Share Posted April 13, 2006 right here you go dude this should be ok<?$number = array();$number[0]=rand(1,6);$number[1]=rand(1,6);$number[2]=rand(1,6);while($number[0] == $number[1] or $number[0] == $number[2] or $number[1] == $number[2]){ $number[0]=rand(1,6); $number[1]=rand(1,6); $number[2]=rand(1,6);}echo $number[0];echo "<br>";echo $number[1];echo "<br>";echo $number[2];?> Quote Link to comment Share on other sites More sharing options...
iandavies Posted April 13, 2006 Author Share Posted April 13, 2006 [!--quoteo(post=364401:date=Apr 13 2006, 02:38 PM:name=predator12341)--][div class=\'quotetop\']QUOTE(predator12341 @ Apr 13 2006, 02:38 PM) [snapback]364401[/snapback][/div][div class=\'quotemain\'][!--quotec--]right here you go dude this should be ok<?$number = array();$number[0]=rand(1,6);$number[1]=rand(1,6);$number[2]=rand(1,6);while($number[0] == $number[1] or $number[0] == $number[2] or $number[1] == $number[2]){ $number[0]=rand(1,6); $number[1]=rand(1,6); $number[2]=rand(1,6);}echo $number[0];echo "<br>";echo $number[1];echo "<br>";echo $number[2];?>[/quote]thats great, thanks! Quote Link to comment Share on other sites More sharing options...
predator12341 Posted April 13, 2006 Share Posted April 13, 2006 [!--quoteo(post=364412:date=Apr 13 2006, 02:54 PM:name=iandavies)--][div class=\'quotetop\']QUOTE(iandavies @ Apr 13 2006, 02:54 PM) [snapback]364412[/snapback][/div][div class=\'quotemain\'][!--quotec--]thats great, thanks![/quote]no probs man glad i could help ya Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 13, 2006 Share Posted April 13, 2006 Here's a better way:[code]<?php$nums = array();$rn = rand(1,6);for ($cnt = 0; $cnt < 3;$cnt++) { while ((in_array($rn,$nums)) $rn = rand(1,6); $nums[] = $rn;}echo '<pre>' . print_r($nums,true) . '</pre>';?>[/code]This code can be scaled up as long as the number of numbers you're looking to generate is less than or equal to the maximum random number.Ken Quote Link to comment Share on other sites More sharing options...
predator12341 Posted April 13, 2006 Share Posted April 13, 2006 [!--quoteo(post=364437:date=Apr 13 2006, 03:36 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 13 2006, 03:36 PM) [snapback]364437[/snapback][/div][div class=\'quotemain\'][!--quotec--]Here's a better way:[code]<?php$nums = array();$rn = rand(1,6);for ($cnt = 0; $cnt < 3;$cnt++) { while ((in_array($rn,$nums)) $rn = rand(1,6); $nums[] = $rn;}echo '<pre>' . print_r($nums,true) . '</pre>';?>[/code]This code can be scaled up as long as the number of numbers you're looking to generate is less than or equal to the maximum random number.Ken[/quote]hi ken i said the way i said because from what i understnad he is very new to php so i thoguht giving him something to confussinf (like you have) would just confusse him even more if you are giving him the above the least you could do is explain it a little as that is quite a bit of info to take in for a newbie (forgive the term ian)regardsMark Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 13, 2006 Share Posted April 13, 2006 The OP may new to PHP, but we don't know about his overall programming skills, besides I think that showing the power of PHP to everyone helps in the long run.There are many different ways of solving the problem. Perhaps I should said "a different way" instead of "a better way".Ken Quote Link to comment Share on other sites More sharing options...
predator12341 Posted April 13, 2006 Share Posted April 13, 2006 [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]The OP may new to PHP, but we don't know about his overall programming skills[/quote]yes that is true i suppose[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]besides I think that showing the power of PHP to everyone helps in the long run.[/quote]yea i think you are correct there to[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]There are many different ways of solving the problem. Perhaps I should said "a different way" instead of "a better way".[/quote]and yea i suppose that would have been a better thing to say lets just say both ways are just as good depends what extent he can understand 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.