noob - array, assign a random number

hi,

I’m trying to assign a random numbers to array that don’t repeat. sounded simple but I can’t figure out why my first number always repeats.

can you have a look at the script and help me out?

thanks

Post your code so we can edit it.

var randomness : Array = Array(23);
var allTiles : Array = Array(23);
var sequeNum :int;
var found:boolean;

function OnMouseUp() 
{

	for (var lloop:int =0; lloop< randomness.length; lloop++)
	{	

		for (var llp:int =0; llp< randomness.length; llp++)
		{
			
			if (sequeNum==	randomness[llp])
			{
				found=true;
			}
			else
			{
				found=false;
			}
			if (found == true)
			{
				sequeNum = Random.Range(0, 23);
				llp=0;
				//found= false;
			}
			
		}
		
		if (found==false)
			{	
				
				randomness[lloop]=sequeNum;
			}
		allTiles[lloop] = "Tile"+randomness[lloop];
		print(allTiles[lloop]);
		
	}
}

cheers

please use [code ] tags. Would make the post neater. and also i suggest tabbing the code for neatness.

For a situation like this, where you want to have every number in the array once each, generating numbers one at a time and then checking against the entire array is very much the slow way to do things. Instead, you want to generate the list and then shuffle it. Also, you generally want to stay away from Array, as it causes issues on some platforms. So for example:

var allTiles : String[] = new String[23];

function Randomize ()
{
	// Generate the list in order
	var i : int;
	for(i = 0; i < allTiles.Length; i++)
		allTiles[i] = "Tile" + i;

	// Now shuffle it
	var j : int;
	var temp : String;
	for(i = 0; i < allTiles.Length; i++)
	{
		j = Random.Range(0, allTiles.Length);
		temp = allTiles[i];
		allTiles[i] = allTiles[j];
		allTiles[j] = temp;
	}
}

thanks a lot Errorsatz. It makes sense. yah, i’m such a noob to all that. :slight_smile: