Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

how to generate 3 unequal numbers?

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

how to generate 3 unequal numbers?

Postby somdcomputerguy » Fri Sep 20, 2013 10:49 am

I need to produce 3 random numbers (from 2 to 5) that aren't the same. The first and second variables are what they are, and the third variable shouldn't equal either one of the first two. I have this code (which doesn't work for me, obviously), and I have tried multiple variations of operators. I can figure out how to do this with a large amount of IF loops, but I really don't want to..

I am at a loss and would appreciate some help.

Code: Select all
<?
do{
   $dAlbumA = rand(2, 3);
   $dAlbumB = rand(4, 5);
   $dAlbumC = rand(2, 5);
  } while (($dAlbumC != $dAlbumA) && ($dAlbumC != $dAlbumB));
echo $dAlbumA . " " . $dAlbumB . " " . $dAlbumC;
?>
somdcomputerguy
New php-forum User
New php-forum User
 
Posts: 14
Joined: Tue Oct 04, 2011 6:50 am
Location: West Virginia

Re: how to generate 3 unequal numbers?

Postby somdcomputerguy » Fri Sep 20, 2013 7:55 pm

Well I did write code that does what I want. I used two If loops in a While loop. Like I said, it does do what I want, but I spent too much time trying to get it to work. I concluded that I would put it all in a While loop, instead of a Do .. While loop, but I spent alot of time trying (unsuccessfully) to check all three variables in one If loop.. Here's the code if it'll help anyone else.
Code: Select all
<?
while (1) {
 $dAlbumA = rand(2, 3);
 $dAlbumB = rand(4, 5);
 $dAlbumC = rand(2, 5);

if ($dAlbumC <> $dAlbumA) {
 if ($dAlbumC <> $dAlbumB) {
     echo $dAlbumA . " " . $dAlbumB . " " . $dAlbumC;
     exit;
  }
 }
}
?>
somdcomputerguy
New php-forum User
New php-forum User
 
Posts: 14
Joined: Tue Oct 04, 2011 6:50 am
Location: West Virginia

Re: how to generate 3 unequal numbers?

Postby johnj » Fri Sep 20, 2013 8:08 pm

Code: Select all
while (1) {
 $dAlbumA = rand(2, 5);
 if ($dAlbumA == '2')
 {
   $dAlbumB = $dAlbum + 1;
   $dAlbumC = $dAlbum + 2;
 }
 elseif ($dAlbumA == '5')
 {
   $dAlbumB = $dAlbum - 1;
   $dAlbumC = $dAlbum - 2;
 }
 else
 {
   $dAlbumB = $dAlbum + 1;
   $dAlbumC = $dAlbum -1;
 }
 echo $dAlbumA . " " . $dAlbumB . " " . $dAlbumC;
}
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: how to generate 3 unequal numbers?

Postby somdcomputerguy » Fri Sep 20, 2013 8:37 pm

Hmm, interesting approach johnj. But the first variable (A) needs to be either 2 or 3, and B needs to be either 4 or 5, while C can be any number 2 thru 5, but not equal to (the same as) A or B. Thanks for the reply though!
somdcomputerguy
New php-forum User
New php-forum User
 
Posts: 14
Joined: Tue Oct 04, 2011 6:50 am
Location: West Virginia

Re: how to generate 3 unequal numbers?

Postby johnj » Sun Sep 22, 2013 12:31 am

We cannot give you all the solutions based on your changing requirements. I showed you one method, please try something similar or think something different.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: how to generate 3 unequal numbers?

Postby somdcomputerguy » Sun Sep 22, 2013 4:13 am

:o

Well I did come up with a solution, see my second post in this thread, and in my first post, I thought I had declared that the declaration of the variables were OK as they were, but I probably wasn't too clear (definitive) on that. But thanks again anyway, for providing some code that shows a different way to coming up with a solution.
somdcomputerguy
New php-forum User
New php-forum User
 
Posts: 14
Joined: Tue Oct 04, 2011 6:50 am
Location: West Virginia


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 3 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.