• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 372
  • Last Modified:

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);

Open in new window

0
Spiderstave
Asked:
Spiderstave
  • 4
  • 4
  • 2
1 Solution
 
igni7eCommented:
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
 
igni7eCommented:
function is like this:

exists(passedNum)
{
for(i=0;i<20;i++){          
       if numberArray[i] = passedNum
            return true;
}
return false;
}
0
 
SpiderstaveAuthor Commented:
Thanks for your reply.

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

Thanks!

Luke
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
igni7eCommented:

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
 
SpiderstaveAuthor 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
 
igni7eCommented:
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
 
SpiderstaveAuthor Commented:
Thanks for your posts, but unfortunately this is still not working.

Does anyone else have any ideas?
0
 
TanLiHaoCommented:
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
 
TanLiHaoCommented:
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
 
SpiderstaveAuthor Commented:
Works great, thank you!
0
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.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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