PDA

View Full Version : how to generate Random Questions for a game?


Blackshark
05-18-2004, 06:01 PM
HI... i'm trying to make a "Who wants to be millionaire" flash game. but the questions are allways the same and in the same order. :rolleyes: I had an idea: let Actionscript choose 1 question out of 100... so the question DONT repeat.
But the problem is: :confused: I dont know how to generate random questions... i just know how to generate random Numbers. plz help

exactpixel
05-18-2004, 06:18 PM
here you go:

//put all your info in an array
var myQuestionArray:Array = new Array();

//this right here will create your random number that
//will grab your question from the array
myQuestions = random(100);

//name the time line you are on
myTimeLine = this;
//your question here:

myText_txt.text = myTimeLine[myQuestionArray[myQuestions]];


now that's if you have the questions appear in a text feild.

hope that helps

pixelwit
05-18-2004, 07:49 PM
I wouldn't recommend picking questions at random since you'll probably end up selecting the same question more than once before you go through all the questions. A better suggestion might be to shuffle the contents of the array then to go through the array in order so you don't repeat any questions.

You should be able to find a shuffle function by searching the forums for "shuffle".

-PiXELWiT
http://www.pixelwit.com

Drakkon
05-18-2004, 08:47 PM
I love shuffle functions...this is about the simplest as they come i think :D

//pre : send argument of array with numbers of questions
//post : Returns array with numbers provided "shuffled"
function shuffle(numArr):Array {
for(num=numArr.length-1;num>0;num--) {
var myPos = Math.round(Math.random()*num);
var tempVar = numArr[num];
numArr[num] = numArr[myPos];
numArr[myPos] = tempVar;
}
return numArr;
}

mpol777
05-19-2004, 01:18 AM
If your questions are going to be held in objects than you can do something like this:


// array to hold all of the questions
questionArray = new Array();

// question object
function QuestionObject() {
// set id to random number
this.id = Math.floor(Math.random()*10000));
this.questionType = null;
this.answeredStatus = null;
this.someParam = null;
}

// create questions
questionArray.push(new QuestionObject());
questionArray.push(new QuestionObject());
questionArray.push(new QuestionObject());

// order array based on random id
questionArray.sortOn("id");


Each object gets a unique id and then using the sortOn array method it reorders them based on that id.

farafiro
05-19-2004, 05:50 AM
and from here u gan get a random value from an array and never duplicate the question
http://proto.layer51.com/l.aspx?p=3

Blackshark
05-23-2004, 03:44 PM
:confused: :confused: :confused:

hmm where do i put the questions then??? in a text field? in a movie clip?.. i'm new to actionscript.

Blackshark
05-23-2004, 03:50 PM
:eek: i got another idea.... HOW can i make the player choose a random frame and play it.... that way i can use multiple choise questions

farafiro
05-24-2004, 03:20 AM
var totalFrames = 5
theLevel_btn.onRelease = function(){
_level0.gototAndStop(Math.rounf(Math.random()*tota lFrames+1))
}

pixelwit
05-24-2004, 04:43 AM
Rather than using "Math.rounf" (with an "F" even ;)), you would be better off using "Math.floor" since Farafiro's code will occasionally produce 6 as a result. Using "floor" guarantees numbers between 1 and 5.

-PiXELWiT
http://www.pixelwit.com

farafiro
05-24-2004, 05:09 AM
yup, sorry for that

scriptprogramer21
08-22-2006, 07:49 PM
hey everyone im wroking on a project almost like a online version of milton bradely's card game uno and i need a script or how to deal out random cards and im not toatally noobie on flash i just cant find anything on how to produce random buttons.. oh and the cards are in "symbol" button format so the user can click the card...

shift
03-01-2007, 07:11 AM
Hi,

I want to make the an ascending random function.

This is the code:

var totalFrames = 7;
this.gotoAndStop(Math.floor(Math.random()*totalFra mes+1));

What i mean is, when the frame choosen is 4 then the next random frame be higher than 4. When it hits the top then the function would stop.

Please let me know if there is a way to do this.

Thanks a lot,
Adrian

darrenleong
04-20-2007, 03:42 AM
hi.. Erm.. i have a problem with my project. When i click the start button right, it will run randomly among the 60 circles and den come to a stop at the random selected circles. i need a script. and i have no knowledge about flash at all.. Hope to hear ur reply asap. thanks


http://download.yousendit.com/B14BEFCC4ED1600F

the game is my example of my project..