jayh99
asked on
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
JaYh99
In what language?
Just numbers 1, 2, 3, 4? And TimYates - this i believe is a PHP question...
<?
$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;
}
}
?>
$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;
}
}
?>
Sample is at www.uniquetraveller.com/random.php
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry I didn't clarify on this, I was hoping to do it in ASP. Thanks for the help.
ASKER
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>
<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>
Oh crap. Why did I think this was PHP only? My bad.
ASKER
B+ for effort! j/k Your solution probably would have worked. I didn't specify a language, have some points. Thanks for the help.
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!
ASKER
Yeah, I am going to use it to randomize the answers to multiple choice questions from a database.