Copy link to clipboard
Copied
Hello all,
I am attempting to assign a random number from 1 - 20 to 20 different variables. I need each variable to have a unique number. I later put the variables in a list and because of the randomly generated numbers the list is in a different order everytime the program is run. What i have works, but i feel it is bulky. I was wondering if there was a more efficient way of achieving this. Thanks:
Here is an exerpt of how i am proceeding, this shows the process for the first 10 variables, but the last 10 follow the same pattern:
n1= Math.floor(Math.random()*20)+1;
questionRandomizer1 ();
questionRandomizer2 ();
questionRandomizer3 ();
questionRandomizer4 ();
questionRandomizer5 ();
questionRandomizer6 ();
questionRandomizer7 ();
questionRandomizer8 ();
questionRandomizer9 ();
questionRandomizer10 ();
questionRandomizer11 ();
questionRandomizer12 ();
questionRandomizer13 ();
questionRandomizer14 ();
questionRandomizer15 ();
questionRandomizer16 ();
questionRandomizer17 ();
questionRandomizer18 ();
questionRandomizer19 ();
questionRandomizer20 ();
function questionRandomizer1 ()
{
QuestionOrder[1] = n1;
}
function questionRandomizer2 ()
{
QuestionOrder[2] = Math.floor(Math.random()*20)+1;
if (QuestionOrder[2] == QuestionOrder[1]){questionRandomizer2 ()}
}
//I am generating a random number for QuestionOrder[2] then checking if it is the same number as QuestionOrder[1] and if it is, i am re-running the function to generate a new random number, until it gets a unique number. This pattern continues for each following function.
function questionRandomizer3 ()
{
QuestionOrder[3] = Math.floor(Math.random()*20)+1;
if (QuestionOrder[3] == QuestionOrder[1] || QuestionOrder[3] == QuestionOrder[2]){questionRandomizer3 ()}
}
function questionRandomizer4 ()
{
QuestionOrder[4] = Math.floor(Math.random()*20)+1;
if (QuestionOrder[4] == QuestionOrder[1] || QuestionOrder[4] == QuestionOrder[2]|| QuestionOrder[4] == QuestionOrder[3]){questionRandomizer4 ()}
}
function questionRandomizer5 ()
{
QuestionOrder[5] = Math.floor(Math.random()*20)+1;
if (QuestionOrder[5] == QuestionOrder[1] || QuestionOrder[5] == QuestionOrder[2]|| QuestionOrder[5] == QuestionOrder[3]|| QuestionOrder[5] == QuestionOrder[4]){questionRandomizer5 ()}
}
function questionRandomizer6 ()
{
QuestionOrder[6] = Math.floor(Math.random()*20)+1;
if (QuestionOrder[6] == QuestionOrder[1] || QuestionOrder[6] == QuestionOrder[2]|| QuestionOrder[6] == QuestionOrder[3]|| QuestionOrder[6] == QuestionOrder[4]
|| QuestionOrder[6] == QuestionOrder[5]){questionRandomizer6 ()}
}
function questionRandomizer7 ()
{
QuestionOrder[7] = Math.floor(Math.random()*20)+1;
if (QuestionOrder[7] == QuestionOrder[1] || QuestionOrder[7] == QuestionOrder[2]|| QuestionOrder[7] == QuestionOrder[3]|| QuestionOrder[7] == QuestionOrder[4]
|| QuestionOrder[7] == QuestionOrder[5] || QuestionOrder[7] == QuestionOrder[6]){questionRandomizer7 ()}
}
function questionRandomizer8 ()
{
QuestionOrder[8] = Math.floor(Math.random()*20)+1;
if (QuestionOrder[8] == QuestionOrder[1] || QuestionOrder[8] == QuestionOrder[2]|| QuestionOrder[8] == QuestionOrder[3]|| QuestionOrder[8] == QuestionOrder[4]
|| QuestionOrder[8] == QuestionOrder[5] || QuestionOrder[8] == QuestionOrder[6] || QuestionOrder[8] == QuestionOrder[7]){questionRandomizer8 ()}
}
function questionRandomizer9 ()
{
QuestionOrder[9] = Math.floor(Math.random()*20)+1;
if (QuestionOrder[9] == QuestionOrder[1] || QuestionOrder[9] == QuestionOrder[2]|| QuestionOrder[9] == QuestionOrder[3]|| QuestionOrder[9] == QuestionOrder[4]
|| QuestionOrder[9] == QuestionOrder[5] || QuestionOrder[9] == QuestionOrder[6] || QuestionOrder[9] == QuestionOrder[7] || QuestionOrder[9] == QuestionOrder[8]){questionRandomizer9 ()}
}
function questionRandomizer10 ()
{
QuestionOrder[10] = Math.floor(Math.random()*20)+1;
if (QuestionOrder[10] == QuestionOrder[1] || QuestionOrder[10] == QuestionOrder[2]|| QuestionOrder[10] == QuestionOrder[3]|| QuestionOrder[10] == QuestionOrder[4]
|| QuestionOrder[10] == QuestionOrder[5] || QuestionOrder[10] == QuestionOrder[6] || QuestionOrder[10] == QuestionOrder[7] || QuestionOrder[10] == QuestionOrder[8]
|| QuestionOrder[10] == QuestionOrder[9]){questionRandomizer10 ()}
}
as you can see the list of checks gets progressively longer. I feel like i am doing a ton of If, Then, Else statements, and usually when that happens i'll use a case system. Im wondering if there is a more efficient way to do this, in the same way a case statement is more efficient than a bunch of If, Then, Else statements.
Thanks.
Just create an array with the 20 values in it and then either randomly select them (and splice afterwards), or probably even better, just shuffle the array and extract the randomly ordered elements in the order they come.
Here is a function for shuffling the array in AS3:
function shuffle(a:Array) {
var p:int;
var t:*;
var ivar:int;
for (ivar = a.length-1; ivar>=0; ivar--) {
p=Math.floor((ivar+1)*Math.random());
t = a[ivar];
a[ivar] = a
;
a
= t;
}
}
// Usage
...Copy link to clipboard
Copied
Just create an array with the 20 values in it and then either randomly select them (and splice afterwards), or probably even better, just shuffle the array and extract the randomly ordered elements in the order they come.
Here is a function for shuffling the array in AS3:
function shuffle(a:Array) {
var p:int;
var t:*;
var ivar:int;
for (ivar = a.length-1; ivar>=0; ivar--) {
p=Math.floor((ivar+1)*Math.random());
t = a[ivar];
a[ivar] = a
;
a
= t;
}
}
// Usage:
shuffle(anArray);
Copy link to clipboard
Copied
That worked brilliantly, thank you.
one question: I understand what is happening, but i have not seen a variable declared as type " * " before. What does this represent?
Copy link to clipboard
Copied
Consider it a wildcard, where it can represent any class. The types of objects that are stored in the array are not known by the function - nor are they important. Using the asterisk satisfies the formality of declaring the variable type without knowing or caring what it might be.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now