Jump to content

Archived

This topic is now archived and is closed to further replies.

iandavies

how to generate 3 unqiue random numbers

Recommended Posts

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

Share this post


Link to post
Share on other sites
[!--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)

Share this post


Link to post
Share on other sites
[!--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

Share this post


Link to post
Share on other sites


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];


?>

Share this post


Link to post
Share on other sites
[!--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!

Share this post


Link to post
Share on other sites
[!--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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[!--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)
regards
Mark

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[!--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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.