Posts Tagged ‘Math.Random()’

Flash app used to pick winner.

Monday, March 16th, 2009

We recently selected @matthewwithanm as the winner of the Flashpitt suggestion contest, he will receive a complimentary ticket to Flashpitt.

Prior to choosing a winner we had to decide, just that, how we were going to choose the winner. The obvious way would have been to print off everyones names, cut them up, put them in a hat and pick one. Since it is 2009 I figure I can do better than the analog way and I don’t have to risk trees, paper cuts, or loss of fingers (my scissor skills are not the best), so out comes flash and a 20 min code session begins.

I came up with this little gem, where you enter the number of entries you have and a number greater than or equal to 1. The application uses this number to select a random number to decide how many times the Math.random function is ran before the winner is selected.

i.e. say you put 50 in that section the Application grabs a random number between 1 and 50, say 44, then determines that the 44th random number selected will be the winner.

Test it out here.

This movie requires Flash Player 9

I know it’s pretty lame but maybe it will help you in selecting a winner for some drawing your doing. Plus I wanted to post it so when I do another drawing for Flashpitt I can just use this one.

Click here to download the source.

Happy Flashing.

Create an Array of Random Numbers, Without Reusing Numbers

Monday, December 10th, 2007

I recently had the situation where I had to create an array of 25 random numbers from 0-24, but no number could be repeated in the array. So after a little bit of racking my brain I came up with this.

//create an array to hold the numbers
var numArray:Array = new Array(25);

// create an array to hold the used numbers
var usedNumArray:Array = new Array(25);

for (var i:int = 0; i <= 24; i++){
	pickNum(i);
}

// run fuctions to get a random number and put in it the array
function pickNum(i:Number){
	var num:Number;
	do{
		//set num to a random number between 0-24
		num = getRandNum();
		//set the numArray[i] to the random number
		numArray[i] = num;
		//make sure the number is a new number
	}while(usedNumArray[num]);
	//if its a new number set the usedNumArray[num] to true
	usedNumArray[num]=true;
	//trace it back to make sure it works
	if(i == 24){
	trace(numArray)
	}
}

// get random number
function getRandNum():Number{
	return Math.floor(Math.random()*25);
}

I don't know if its the best way to handle it but it works. If there is a better way please let me know.