Unique random # func - Flash
- Started
- Last post
- 1 Response
- guiles
I'm trying to figure out how to generate a unique number and display content from an array.
I've got it set up to when you click a button it runs the askAgain(); function (see code below) and displays the result in a dynamic text field.
--
quesArray = new Array();
quesArray[1] = "1";
quesArray[2] = "2";
quesArray[3] = "3";
quesArray[4] = "4";
quesArray[5] = "5";
quesArray[6] = "6";
quesArray[7] = "7";
quesArray[8] = "8";
quesArray[9] = "9";
quesArray[10] = "10";
quesArray[11] = "11";
askAgain = function () {
q = random(11)+1;
question = quesArray[q];
};
askAgain();
--
Now when I click the button, sometimes it generates the SAME result. I want it to be a unique result everytime. Ihope this makes sense, thanks for your help.If you want to see my example go here: http://www.osu-okmulgee.edu/acad…
Once everything loads click on the "?" button above the character on the left.... that is what I'm doing.
- SmilingBuddh0
Here's an Array prototype that will return a random index from the array (and delete that index):
Array.prototype.spitRandom = function() {
var i =random(this.length)
var spit = this[i];
this.splice(i,1);
return(spit);
}In your example, you can then use
var answer = quesArray.spitRandom();
to give you a random value.
Cheers.