Random Array String

Hello there!

I’m generating some random numbers and I’m putting them inside an array. How do I go about so no number repeats itself?
I have an array with the random generated numbers, which first i’m sorting.
If it find two numbers which are the same, it randomly generates the second number.

The result is then inserted into another vector.

function PozitiiRandom()
{
oldarrPositions = new Array();
newarrPositions = new Array();

var r:System.Random = new System.Random();
for(var j:int=0;j<pozitii.length;j++)
 {
 var k:int =r.Next(pozitii.length);
 oldarrPositions.Push(k);
 }

oldarrPositions.Sort();

 //print("OldArrayLength="+oldarrPositions.length);
 var m:int=0;
 for (var i:int=0;i<oldarrPositions.length;i++)
 {
	oldarrPositions.Sort(); 
	 print("OldArray="+oldarrPositions);
	 while ((i>0)  (oldarrPositions[i] == oldarrPositions[i-1]))
	 {
		 
		  oldarrPositions[i] = r.Next(oldarrPositions.length);
		
		//  newarrPositions.Splice(i,1,oldarrPositions[i]);
		 
		
		// m--;
	   //  newarrPositions.Push(oldarrPositions[i]);
	 //m++;
	  
	 }
	 newarrPositions[m]=oldarrPositions[i];
 	 m++;

 }

One way to do it is to create a ‘while’ loop and use keyword ‘in’ to check for array membership:

while (r in array) r = Random.Range(0, 100);

I’ve changed the initial code. I am adding in one vector only the random numbers that are not already in that vector. I used your code and it worked.

Thank you.