5 Replies Latest reply on Jun 27, 2007 2:48 PM by kglad

    Randomly sorting an array

    MikeD. Level 1
      Okay, I have a string array that contains answers to multiple questions. In my example data I'm working with, there are always 3 responses in the array. I want to write them out to the screen in random order, since when the array is created the "correct" answer is always array position 0 (ie, the first one).

      The array is called "responsesArray". I was using the attached code to try to create a new array called "sortedResponsesArray".

      It APPEARED to be working, but then I noticed that it seemed like the first answer was the correct answer an inordinate amout of times. So I did some testing by running it over and over a couple of hundred times, and in fact it does not appear to be returning random results. The first position in sortedResponsesArray IS in fact the correct answer way too often for it to be random. It appears to be about a 25-5-1 ratio. Ie, if I run it 31 times, 25 times the correct answer will be in the first position of sortedResponsesArray, 5 times the correct answer will be in the second position in sortedResponsesArray, and 1 (or maybe 2) times the correct answer will be in the third position in sortedResponsesArray. It's not an EXACT ratio, but it's close to that...

      Just wondering if anyone can see anything wrong with my sorting logic and/or can suggest anything better..

      Thanks!
      Mike