Preventing same Random.Range in a loop..

Hi programmer buddies,

I wrote a function to make some randomly selected objects on and off. the problem is that sometimes Random.Range returns the same previously selected integer and so I miss a step in the loop and the result (number of activated/deactivated objects) won’t match the random number which feeded to the function:

here’s the code:

function showRandomButton(_rand)
	{
		//print(_rand);
		for( var i = 0; i < _rand; i++)
			{
				//print(i);
				var arrayRand = Random.Range(0, 30);
				print(arrayRand);
				_button[arrayRand].renderer.material.color = Color.red;
				doCountDown = true;
			}
		//wait a few seconds and let the user see the active spheres and count them
		yield new WaitForSeconds(showDelay);	
		//turn off the spheres and let user guess the numbers of actived balls ;)
		buttonReset();
		//round the Round :)
		round++;
	}

assume that _rand is 6. so the “for” loop should loops 6 times, and i should have six different arrayRend. but sometimes i get one or several similar arrayRand and miss that FOR cycle to on/off the objects…

Thanks in advance.

There’s a script in this thread that implements a random picking/shuffling library. One of the functions called SampleRemove will choose a list of integers from a range without picking the same one twice.

Thanks andeeee. I’ve posted my question in other topic to keep things nice and ordered. please take a look…