jeudi 21 avril 2016

How to randomly generate every one of a sequence of numbers in a fast manner

I don't know how to get around a problem I am really struggling with.

There are 72 trials in my program. Each trial, two images gets presented, one image gets taken from one group of 6. The other image gets taken from another group of 6. Therefore, there are 36 possible combinations of pictures (if my groups of images are ABCDEF and GHJKIL, combinations would be AG, AH, AJ, etc.). I want each combination to be presented twice, in a random sequence. So, each combination will be presented twice, and only twice. I am doing this by writing a switch statement.

Each trial I generate a random number from 0 to 35. There are 36 cases in the switch statement (I know this is a lot, but I really can't think of any other way around this). The value in the switch statement is an integer. One case will be satisfied each trial, and in that case there will be code to display one of the 36 unique combinations. At the end of the trial, I decrement an element in an integer array that is of size 36. Before the trials start, each value in the array is initialized as 2. When an element reaches zero, the case that corresponds to that position in the array can no longer be called (because that unique combination has been displayed twice).

For instance, say in one trial, the number 23 is randomly generated. The below portion of the code gets called

switch(randomNum)
    ....
    case(23){
        display the 23rd unique combination of stimulus elements
        trials[23]--;
    ...
    }

The 23rd unique combination is displayed. Therefore, the 23rd integer in the "trials" integer array is decremented (so, say this combination had not been displayed yet, after decrementing, the value for the 23rd position would now be 1).

This is what I am having trouble with: what happens, when, say, most of the values in the trials integer array are zero? If I write a while loop, say, to keep generating a random number until a number is generated that does not correspond to an array element with the value zero, this is going to take a long time the more trials I perform. Say, for instance, I have done 71 trials. The only combination that can still be displayed is the 15th combination. However, random numbers could keep being generated over and over that are not 15, and so this while loop could potentially loop hundreds of times before the number 15 is obtained? This will be incredibly slow, and my program needs to be fast. How can I get around this problem?

The only thing I could think of would be to keep reducing the amount of random numbers that could be generated for the randomNum. For instance, if the number 30 has been generated twice already, then I could somehow not allow that number to be generated. Then when 22 comes up twice, both 30 and 22 could not be generated. However, from searching these forums, I have read that it is not possible to generate a random number from specified numbers (e.g., there is no function that you can tell to generate a random number from 0 to 35 but excluding 22 and 30. Math.random() can only generate random numbers from a specified range). Thanks for your help.




Aucun commentaire:

Enregistrer un commentaire