Learn how to a build a cloud-first strategyRegister Now

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 350

# AS2 - How to make each value in an Array unique

Hello,

I'm trying to create an array full of random numbers, but each value in the array must be unique, and I'm not sure how to accomplish this. I know how to create an array and fill it with random numbers, but sometimes the same number will appear multiple times. Does anyone know how to do this? I'm posting the code I have so far below.

Any ideas are appreciated.
``````var numberArray:Array = new Array();
var randNumber:Number;

for(i=0;i<20;i++){
randNumber = Math.floor(Math.random() * 40);
numberArray[i] = randNumber;
}

trace(numberArray);
``````
0
Spiderstave
• 4
• 4
• 2
1 Solution

Commented:
for(i=0;i<20;i++){
//New logic:
while( exists(randNumber))
randNumber = Math.floor(Math.random() * 40);
numberArray[i] = randNumber;
}

new method called exists(passedNumber)
uses a for loop to check passedNumber
if its found return true
and after the loop, if it hasnt returned, then return false
0

Commented:
function is like this:

exists(passedNum)
{
for(i=0;i<20;i++){
if numberArray[i] = passedNum
return true;
}
return false;
}
0

Author Commented:

I'm unable to get your script functioning, it's throwing errors. Would you mind re-checking your code?

Thanks!

Luke
0

Commented:

Here is the function:
exists = function(passedNum)
{
for(i=0;i<20;i++){
if numberArray[i] = passedNum
return true;
}
return false;
}

Depending on where the numberArray is declared, you may need to pass it to the function just like passedNum is.
0

Author Commented:
Hmm, is this for Actionscript 2? I've tested it and it's not working, and it appears that the syntax is incorrect in several places.
0

Commented:
for(i=0;i<20;i++){
//New logic:
while( exists(randNumber))
{
randNumber = Math.floor(Math.random() * 40);
}
numberArray[i] = randNumber;
}

exists = function(passedNum)
{
for(i=0;i<20;i++){
if (numberArray[i] = passedNum)
{
return true;
}
}
return false;
}

Sorry, I cant test this, but I tried fixing it up.
0

Author Commented:
Thanks for your posts, but unfortunately this is still not working.

Does anyone else have any ideas?
0

Commented:
Here's a neat trick I used before.

var numberArray:Array = new Array();
var randNumber:Number;

for(var i:Number = 0;i < 40; i++){
do {
randNumber = Math.floor(Math.random() * 40);
} while(numberArray[randNumber] != undefined);
numberArray[randNumber] = i;
}
numberArray = numberArray.slice(0, 19);

trace(numberArray);
0

Commented:
For igni7e's code to work, there are a number of things he actually need to do. He need to instantiate the array and the number. Otherwise it will fail. Furthermore he used an assignment expression where a equality expression is expected.

Therefore if you want to use his code, I have fixed it for him, it's way longer though I do not if it's more efficient.

Here it is:

var numberArray:Array = [];
var randNumber:Number = 0;

for(var i:Number =0;i<20;i++){
while(exists(randNumber))
{
randNumber = Math.floor(Math.random() * 40);
}
numberArray[i] = randNumber;
}

function exists(passedNum:Number):Boolean {
for(var i:Number = 0;i < numberArray.length;i++){
if (numberArray[i] == passedNum) {
return true;
}
}
return false;
}
trace(numberArray);
0

Author Commented:
Works great, thank you!
0

## Featured Post

• 4
• 4
• 2
Tackle projects and never again get stuck behind a technical roadblock.