PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Select a random subset of Array
A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Select a random subset of Array

  1. #1
    Member
    Join Date
    Sep 2005
    Posts
    64

    Select a random subset of Array

    Howdy Flashkit,

    I've imported questions for a quiz from XML successfully.

    Actionscript Code:
    var myXML:XML;
    var myLoader:URLLoader = new URLLoader();
    myLoader.load(new URLRequest("../xml/Quiz.xml"));
    myLoader.addEventListener(Event.COMPLETE, processXML);

    function processXML(e:Event):void {
        myXML = new XML(e.target.data);
        for (var i:int = 0; i<myXML.*.length(); i++) {
           
            var myQuestion:XMLList = myXML.quest[i].question;
            var myAnswer:XMLList = myXML.quest[i].answer;
           
            trace(myXML);
        }
    }

    What I'd like to do next is create a subset of 10 randomly chosen (shuffled and unique) questions from the bigger group (say of 21 questions). What is the best way to do this? I'm guessing I'll be using an Array.

    Many thanks for any thoughts on this!


  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Yep. Arrays.

    I assume you do not want to repeat your questions, so you will want to remove the selected ones from the pool after you select them.

    Although probably not strictly necessary, I'm going to first take the XMLList representing the questions and answers and put each of those quest nodes in an array. Then we'll be selecting from and removing from that array to our selected questions array.

    Code:
    var myXML:XML;
    var myLoader:URLLoader = new URLLoader();
    myLoader.load(new URLRequest("../xml/Quiz.xml"));
    myLoader.addEventListener(Event.COMPLETE, processXML);
    var rawQuestions:Array = [];
    var selectedQuestions:Array;
    var numberToSelect:int = 10;
    
    function processXML(e:Event):void {
        myXML = new XML(e.target.data);
        for (var i:int = 0; i<myXML.*.length(); i++) {
            rawQuestions.push(myXML.quest[i]);
        }
        selectedQuestions = selectQuestions(numberToSelect);
    }
    
    function selectQuestions(int:howMany):Array{
      var toreturn:Array = [];  //array of Question object
      for (var i:int = 0; i < howMany; i++){
        var qXML:XML = removeRandomElement(rawQuestions);
        var q:Question = new Question(qXML.question, qXML.answer);
        toreturn.push(q);
      }
      return toreturn;
    }
    
    function removeRandomElement(arr:Array):*{
      var idx:int = Math.floor(Math.random()*arr.length);
      var el:* = arr.splice(idx, 1)[0];
      return el;
    }
    I leave the creation of the Question class to you. You don't technically need it, but it sure does make it easier to keep your related data together.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center