Link to home
Start Free TrialLog in
Avatar of Spiderstave
Spiderstave

asked on

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

Avatar of igni7e
igni7e
Flag of Ireland image

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
function is like this:

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

ASKER

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

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

Does anyone else have any ideas?
ASKER CERTIFIED SOLUTION
Avatar of TanLiHao
TanLiHao
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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);
Works great, thank you!