Skip to main content
saratogacoach
Inspiring
September 12, 2012
Answered

Random Array without Sequential Repeats

  • September 12, 2012
  • 1 reply
  • 6755 views

Hi,

Continuing to build the eLearning exercise (much help from kglad). Ran into a problem with sequential repeats in the random array, needing a solution. The AS3 script (much help here from kglad) to generate and then shuffle the array, often (since there are only 9 potential elements for an array length of 70) produces sequential repeats. For example, using 1,2,3,4,5,6,7,8,9 as elements, the shuffled final array can produce 2,2,2,1,4,5,9,3,3.......

Problem is that when these repeats are used in  textbox.text, there is no transition between same (repeated) "words." If a word changes (for example from 5 to 3), the user sees a visiable change. However, with these repeats (for example, 3 to 3), there is no transition: the repeat appears to just stay the same for a longer time. Then the user isn't sure if they've already chosen this word.

In any  event, it would be better, since I can't seem to add a visible transition for repeats, to simply avoid sequential repeats, if possible. But cannot figure out a way to add this to the script.

Any help appreciated.

The current random array and shuffle scripts:

var index:int = 0;

var numToDisplay:int = 10;

var groupA:Array = ["1","2","3"]

var groupB:Array = ["4","5","6"]

var groupC:Array = ["7","8","9"]

var word_array:Array = [];

var i:int;

for(i=0;i<3;i++){

word_array.push(groupA[Math.floor(groupA.length*Math.random())]);

}

for(i=0;i<3;i++){

word_array.push(groupB[Math.floor(groupB.length*Math.random())]);

}

for(i=0;i<3;i++){

word_array.push(groupC[Math.floor(groupC.length*Math.random())]);

}

shuffle(word_array);

function shuffle(a:Array) {

        var i:int;

        var j:int;

        var e:*;

        var len:int = a.length;

        for (i = len-1; i>=0; i--) {

            j=Math.floor((i+1)*Math.random());

            e = a;

            a = a;

            a = e;

        }

}

This topic has been closed for replies.
Correct answer sinious

Yes, sorry about that. Must have mis-counted.

I'll try integrating it into the script that I already have, see if I can get it to work in the exercise.

Much thanks!


You're welcome and if you're all set please mark the thread as answered so we can filter unanswered questions. Good luck!

1 reply

sinious
Legend
September 12, 2012

Are you literally doing the arrays as small as above or is that pseudo? If so you should just shuffle groupA/B/C and add them iteratively (word_array.push(groupA)). That will shuffle the 3 numbers and guarantee no duplicates because you're not randomly picking a number, you're using them all, in their shuffled order.

I'm not familiar with exactly what you're trying to do besides what I see there which is apparently generate an array with 1-9 shuffled. I don't understand why you're breaking them up into 3 groups unless this is pseudo and not actually the real code. I presume you want to add a lot more, potentially duplicate numbers to each group to go this route because it's overkill to just generate a random list of numbers 1-9.

saratogacoach
Inspiring
September 12, 2012

Hi,

Sorry for not providing more detail. The final mixed array of 70 is made up of a ratio of items from the 3 groups (A,B,C) that is 20:20:30. The numbers used in this illustration are stand-in's for the words. But, Group A, Group B and Group C each only have 3 members. So repeats are difficult to avoid.

The rest of the script has timer, setTimeout's in order to quickly present the words to the user who then in split second (600ms with an additional 400ms ,plus 100ms for an O or X to show correct/incorrect, until the next word is displayed). The word transitions are difficult to manage in this short a time. When there is a sequential repeat (3,3 or 7,7, etc.) then there isn't any noticeable visual change between the 2 same words. Since choosing (by pressing or not pressing a button) is required in the exercise, the user can become confused whether they are seeing the same word they have already reacted to (press or no-press). So, one way to prevent this would be to avoid sequential repeats. Another way, which I can't figure out how to implement is to get a very brief duration blink between words when they transition to indicate to the user that a change has occured. I'm a  social worker with modest self-taught scripting skills. So, very challenging. But I'm always game to learn.

Thank you for your interest and help.

sinious
Legend
September 12, 2012

Can you explain the relevance of breaking it into 3 groups?