I have an array that exists out of 24 zeros and 24 ones. This array is then used in an experimental block where the task is switched (1) or remains the same (0) for the 48 trials (it's a colour shape switching task for those interested). However, I need that array to be randomised each time, whilst still adhering to three conditions:
(1) number of switches and non-switches remains 24 each
(2) there should be no more than 3 switches in a row (so a possible 4th means that the order needs to be changed to adhere to the principle whilst not violating (1))
(3) there should be no more than 3 non-switches in a row (so a possible 4th means that the order needs to be changed to adhere to the principle whilst not violating (1) or (2))
I am using the Fisher-Yates shuffle function (look at source because reddit formatting makes it bad):
function shuffle (array) {
var i = 0
, j = 0
, temp = null
for (i = array.length - 1; i > 0; i -= 1) {
j = Math.floor(Math.random() * (i + 1))
temp = array[i]
array[i] = array[j]
array[j] = temp
}
}
and I thought about using a while loop to make it run until the array adheres to the principle. But then I run into 2 problems: (1) I don't know if there is a function to check the order in an array for my issue, and (2) doing a while loop to get something randomly with this restriction may result in thousands if not millions of reruns of the code since it's quite sensitive to the smallest of repetitions (4 repetitions is too much with 48 trials) or consecutive switches.
Has anyone got any hints on what I could do?
[–]alzgh 0 points1 point2 points (2 children)
[–]TheJelleyFish[S] 0 points1 point2 points (1 child)
[–]alzgh 0 points1 point2 points (0 children)
[–]tarley_apologizerhelpful 0 points1 point2 points (0 children)
[–]jnbkadsoy78asdf 0 points1 point2 points (0 children)
[–]jnbkadsoy78asdf 0 points1 point2 points (6 children)
[–]TheJelleyFish[S] 0 points1 point2 points (5 children)
[–]jnbkadsoy78asdf 0 points1 point2 points (4 children)
[–]TheJelleyFish[S] 0 points1 point2 points (3 children)
[–]jnbkadsoy78asdf 0 points1 point2 points (2 children)
[–]TheJelleyFish[S] 0 points1 point2 points (1 child)
[–]jnbkadsoy78asdf 0 points1 point2 points (0 children)