## how to generate 3 unequal numbers?

Moderators: egami, macek, gesf

somdcomputerguy
New php-forum User
Posts: 14
Joined: Tue Oct 04, 2011 6:50 am
Location: West Virginia
Contact:
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
Posts: 14
Joined: Tue Oct 04, 2011 6:50 am
Location: West Virginia
Contact:
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;
}
}
}
?>``````

johnj
php-forum Super User
Posts: 1803
Joined: Thu Mar 10, 2011 5:07 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;
}
``````

somdcomputerguy
New php-forum User
Posts: 14
Joined: Tue Oct 04, 2011 6:50 am
Location: West Virginia
Contact:
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!

johnj
php-forum Super User
Posts: 1803
Joined: Thu Mar 10, 2011 5:07 pm
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.

somdcomputerguy
New php-forum User
Posts: 14
Joined: Tue Oct 04, 2011 6:50 am
Location: West Virginia
Contact:

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.