# Randomize Numbers 1-4

I am wanting some code that will randomize numbers 1, 2, 3, 4 and return unique results (ex: 1 3 4 2 or 2 1 4 3 or 4 3 2 1... etc.).  I don't think it is too terribly difficult, but I haven't had much with getting the numbers to only appear once.  Thanks in advance for the help.

JaYh99
LVL 1
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
In what language?
0
Commented:
Just numbers 1, 2, 3, 4?  And TimYates - this i believe is a PHP question...
0
Commented:
<?

\$a=0;
\$b=0;
\$c=0;
\$d=0;

// variables \$a thoruhg \$d represent whether numbers 1, 2, 3, and 4 have been already chosen or not, to avoid duplication. Or you could use an array like \$check[1], \$check[2] etc.etc.

while(\$a!=1 || \$b!=1 || \$c!=1 || \$d!=1)
{

\$number = rand(1,4);

switch(\$number)
{
case 1:

if(\$a==0)
{// go through this ONLY if \$a hasnt been changed to 1 yet.
\$a++;
echo \$number;
}break;

case 2:
if(\$b==0)
{// go through this ONLY if \$b hasnt been changed to 1 yet.
\$b++;
echo \$number;
}break;

case 3:
if(\$c==0)
{// go through this ONLY if \$c hasnt been changed to 1 yet.
\$c++;
echo \$number;
}break;

case 4:
if(\$d==0)
{// go through this ONLY if \$d hasnt been changed to 1 yet.
\$d++;
echo \$number;
}
break;

}

}

?>
0
Commented:
0
Commented:
Sorry, the above version ONLY outputs the numbers in a row, I'm sure that's not all you needed.

This one will save the numbers in \$first,\$second,\$third,\$forth variables for future use as you wish.

<?

\$a=0;
\$b=0;
\$c=0;
\$d=0;
\$i=1;
// variables \$a thoruhg \$d represent whether numbers 1, 2, 3, and 4 have been already chosen or not, to avoid duplication. Or you could use an array like \$check[1], \$check[2] etc.etc.

while(\$a!=1 || \$b!=1 || \$c!=1 || \$d!=1)
{

\$number = rand(1,4);

switch(\$number)
{
case 1:

if(\$a==0)
{// go through this ONLY if \$a hasnt been changed to 1 yet.
\$a++;

switch(\$i)
{
case 1:
\$first = \$number;
break;

case 2:
\$second = \$number;
break;

case 3:
\$third = \$number;
break;

case 4:
\$forth = \$number;
break;

}

\$i++;
}break;

case 2:
if(\$b==0)
{// go through this ONLY if \$b hasnt been changed to 1 yet.
\$b++;
switch(\$i)
{
case 1:
\$first = \$number;
break;

case 2:
\$second = \$number;
break;

case 3:
\$third = \$number;
break;

case 4:
\$forth = \$number;
break;

}

\$i++;

}break;

case 3:
if(\$c==0)
{// go through this ONLY if \$c hasnt been changed to 1 yet.
\$c++;
switch(\$i)
{
case 1:
\$first = \$number;
break;

case 2:
\$second = \$number;
break;

case 3:
\$third = \$number;
break;

case 4:
\$forth = \$number;
break;

}

\$i++;

}break;

case 4:
if(\$d==0)
{// go through this ONLY if \$d hasnt been changed to 1 yet.
\$d++;
switch(\$i)
{
case 1:
\$first = \$number;
break;

case 2:
\$second = \$number;
break;

case 3:
\$third = \$number;
break;

case 4:
\$forth = \$number;
break;

}

\$i++;

}
break;

}

}

echo \$first.\$second.\$third.\$forth;

?>
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Sorry I didn't clarify on this, I was hoping to do it in ASP.  Thanks for the help.
0
Author Commented:
I found the code online.  Thanks again for all of your help.

<html>
<title>Codeave.com(Unique Random Numbers)</title>
<body bgcolor="#FFFFFF">
<%
' Determines how many unique random numbers to be produced
tot_unique=4

' Determines the highest value for any unique random number
top_number=4
dim random_number, counter, check, unique_numbers
' When passing a varible for an array use redim
redim random_number(tot_unique)
' begin random function
randomize
' Begin a for next loop from one to the max number of unique numbers
For counter = 1 to tot_unique
' select a number between 1 and the top number value
random_number(counter) = Int(Rnd * top_number)+1
' For next loop to compare the values stored in the array to
' the new random value being assigned
for check=1 to counter-1
if random_number(check)= random_number(counter) then
' If the current value is equal to a previous value
' subject
counter=counter-1
end if
next ' Repeat loop to check values
next ' Repeat loop to assign values to the array
%>

<p>
<ol><% 'write out the unique numbers in a list for display
For counter = 1 to tot_unique
response.write "<li>" & random_number(counter) & "</li>"

next
%>
</ol>

</body>
</html>
0
Commented:
Oh crap. Why did I think this was PHP only? My bad.
0
Author Commented:
B+ for effort! j/k Your solution probably would have worked.  I didn't specify a language, have some points.  Thanks for the help.
0
Commented:
Yep it did work. Tested it out myself. That's the PHP method.. it can be pretty flexible when you do work with it - you can use the variables generatd throughout the page and display other 4 objects in a random order as well. Good luck!
0
Author Commented:
Yeah, I am going to use it to randomize the answers to multiple choice questions from a database.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.