Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# Randomize Numbers 1-4

Posted on 2006-04-09
Medium Priority
182 Views
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
0
Question by:jayh99
• 6
• 4

LVL 35

Expert Comment

ID: 16415193
In what language?
0

LVL 4

Expert Comment

ID: 16415289
Just numbers 1, 2, 3, 4?  And TimYates - this i believe is a PHP question...
0

LVL 4

Expert Comment

ID: 16415422
<?

\$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

LVL 4

Expert Comment

ID: 16415427
0

LVL 4

Accepted Solution

jeffkee1985 earned 120 total points
ID: 16415445
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

LVL 1

Author Comment

ID: 16416586
Sorry I didn't clarify on this, I was hoping to do it in ASP.  Thanks for the help.
0

LVL 1

Author Comment

ID: 16416680
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

LVL 4

Expert Comment

ID: 16421187
Oh crap. Why did I think this was PHP only? My bad.
0

LVL 1

Author Comment

ID: 16421481
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

LVL 4

Expert Comment

ID: 16422388
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

LVL 1

Author Comment

ID: 16422782
Yeah, I am going to use it to randomize the answers to multiple choice questions from a database.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility stâ€¦
Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certaâ€¦
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Booleâ€¦
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensuâ€¦
###### Suggested Courses
Course of the Month15 days, 19 hours left to enroll