Skip to content

Instantly share code, notes, and snippets.

@FrancescoMaisto
Last active December 12, 2015 05:28
Show Gist options
  • Save FrancescoMaisto/4721569 to your computer and use it in GitHub Desktop.
Save FrancescoMaisto/4721569 to your computer and use it in GitHub Desktop.
Returns a 4 elements vector containing four different integers ranging from 1 to max, in random order.
package utils
{
/** Returns a 4 elements vector containing four different integers ranging from 1 to max, in random order. */
public function pick4RandomNumbers(max:uint):Vector.<uint>
{
// Create and populate a Vector to get 'max' different numbers from
var numberContainer:Vector.<uint> = new Vector.<uint>();
for (var i:uint = 1; i <= max; i++)
{
numberContainer.push(i);
}
// If you only want the four numbers to be either 1,2 or 3, add a 4th element at random
if (max == 3) numberContainer.push(getRandomNumber(1, max));
//trace("pick4Randomnumbers > numberContainer =" + numberContainer);
// Create and populate a Vector with 4 numbers picked from the numberContainer, at random
var numberPicked:Vector.<uint> = new Vector.<uint>(4, true );
for (i = 0; i < 4; i++)
{
var randomPick:uint = getRandomNumber(0, numberContainer.length - 1);
numberPicked[i] = numberContainer[randomPick];
numberContainer.splice(randomPick, 1);
//trace("pick4Randomnumbers > numberPicked[i] =" + numberPicked[i]);
//trace("pick4Randomnumbers > numberContainer =" + numberContainer);
}
return numberPicked;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment