6 Replies Latest reply on Feb 13, 2009 2:43 PM by ntsiii

    Pulling a random subset out of an XML file??

    Handycam Level 1
      For my quiz, lets say I have an XML file with 200 questions, but I want my actual quiz to have 20 questions, chosen randomly from the pool of 200.

      What's the best way to do this?
        • 1. Pulling a random subset out of an XML file??
          Gregory Lafrance Level 6
          Load the questions into an XMLListCollection, and then use getItemAt(index) with 20 randomly generated indexes that are from 0 to length-1.
          • 2. Re: Pulling a random subset out of an XML file??
            Handycam Level 1
            quote:

            Originally posted by: Greg Lafrance
            Load the questions into an XMLListCollection, and then use getItemAt(index) with 20 randomly generated indexes that are from 0 to length-1.


            How do I do this with 20 randomly generated indices? Do it 20 times?
            • 3. Re: Pulling a random subset out of an XML file??
              Handycam Level 1
              quote:

              Originally posted by: Greg Lafrance
              Load the questions into an XMLListCollection, and then use getItemAt(index) with 20 randomly generated indexes that are from 0 to length-1.


              How do I do this with 20 randomly generated indices? Do it 20 times?
              • 4. Pulling a random subset out of an XML file??
                Gregory Lafrance Level 6
                Just have a loop:

                var my20Ques:XMLListCollection;

                for(var x:uint=0; x<20; x++){
                var num:uint = CREATE_RANDOM_NUMBER();
                my20Ques.addItem(my200QUES.getItemAt(num));
                }

                where CREATE_RANDOM_NUMBER is your code to generate the random number, probably using the Math.random() function in some way, though off the top of my head I don't know exactly.
                • 5. Re: Pulling a random subset out of an XML file??
                  Richard_Abbott
                  You'd also want some way to test if you had already chosen the question and then resample if so ... assuming you don't want repeats.

                  At the cost of a bit more memory I'd think about setting up a 200-long boolean array, initially set all to false. When you choose a question, set the corresponding index flag to true. Then you can test subsequent trials to see if that index is true or false... and then decide how to resample.

                  There are basically two ways to do that. Either you simply pull out another random number and keep trying until you get a free one (which for 20 qs out of 200 won't take long) or else you just step forward through the array, wrapping at 199, until you find a free slot.

                  Another choice instead of 200 booleans would be to have a list of integers which holds the selected values. Then you just test using indexOf to see if you are duplicating.

                  Hope that helps,
                  Richard
                  • 6. Re: Pulling a random subset out of an XML file??
                    ntsiii Level 3
                    There is a random function in the Math library, I don't recall the precise syntax.

                    But as Richard points out, Handycam almost certainly does not want true "random", which allows for repeats, and will need to follow one of Richards suggestions to avoid that.

                    I might use an "associative array" ( Object or hashtable) to store the chosen questions. then:
                    oChosen[questionId] will be null if the question id is not already in the hashtable.