Jump to content


Photo

how to generate 3 unqiue random numbers


  • Please log in to reply
9 replies to this topic

#1 iandavies

iandavies
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 13 April 2006 - 12:59 PM

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

#2 predator12341

predator12341
  • Members
  • PipPipPip
  • Advanced Member
  • 87 posts
  • LocationCheshunt, Hertfordshire, England

Posted 13 April 2006 - 01:26 PM

[!--quoteo(post=364380:date=Apr 13 2006, 01:59 PM:name=iandavies)--][div class=\'quotetop\']QUOTE(iandavies @ Apr 13 2006, 01:59 PM) View Post[/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)
[a href="http://www.sloganizer.net/en/" target="_blank"][/a]

#3 iandavies

iandavies
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 13 April 2006 - 01:31 PM

[!--quoteo(post=364397:date=Apr 13 2006, 02:26 PM:name=predator12341)--][div class=\'quotetop\']QUOTE(predator12341 @ Apr 13 2006, 02:26 PM) View Post[/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

#4 predator12341

predator12341
  • Members
  • PipPipPip
  • Advanced Member
  • 87 posts
  • LocationCheshunt, Hertfordshire, England

Posted 13 April 2006 - 01:38 PM



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


?>

[a href="http://www.sloganizer.net/en/" target="_blank"][/a]

#5 iandavies

iandavies
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 13 April 2006 - 01:54 PM

[!--quoteo(post=364401:date=Apr 13 2006, 02:38 PM:name=predator12341)--][div class=\'quotetop\']QUOTE(predator12341 @ Apr 13 2006, 02:38 PM) View Post[/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!


#6 predator12341

predator12341
  • Members
  • PipPipPip
  • Advanced Member
  • 87 posts
  • LocationCheshunt, Hertfordshire, England

Posted 13 April 2006 - 01:56 PM

[!--quoteo(post=364412:date=Apr 13 2006, 02:54 PM:name=iandavies)--][div class=\'quotetop\']QUOTE(iandavies @ Apr 13 2006, 02:54 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
thats great, thanks!
[/quote]


no probs man glad i could help ya
[a href="http://www.sloganizer.net/en/" target="_blank"][/a]

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 13 April 2006 - 02:36 PM

Here's a better way:
<?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>';
?>

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

#8 predator12341

predator12341
  • Members
  • PipPipPip
  • Advanced Member
  • 87 posts
  • LocationCheshunt, Hertfordshire, England

Posted 13 April 2006 - 03:06 PM

[!--quoteo(post=364437:date=Apr 13 2006, 03:36 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 13 2006, 03:36 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Here's a better way:
<?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>';
?>

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

[a href="http://www.sloganizer.net/en/" target="_blank"][/a]

#9 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 13 April 2006 - 03:16 PM

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

#10 predator12341

predator12341
  • Members
  • PipPipPip
  • Advanced Member
  • 87 posts
  • LocationCheshunt, Hertfordshire, England

Posted 13 April 2006 - 04:51 PM

[!--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

[a href="http://www.sloganizer.net/en/" target="_blank"][/a]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users