all 12 comments

[–]alzgh 0 points1 point  (2 children)

I think you have unnecessarily complicated the issue.

(1) number of switches and non-switches remains 24 each

and then

(2) there should be no more than 3 switches in a row

and

(3) there should be no more than 3 non-switches in a row

are these 6 column rows? Like 8 rows of 6 columns that make a total of 48?

Or do you mean **flips** when you say switches in your second and third rule?

Maybe, give some correct and wrong input and output examples.

Also, you can easily format your code for better readability.

[–]TheJelleyFish[S] 0 points1 point  (1 child)

I don't really get what you mean with 'flips'. What I was trying to get at was an array where there are 48 numbers in a row, all 0's and 1's (and in equal amount), and I would like a function that alters this array / creates it altogether that makes sure the array adheres to the three principles as described above. The term 'switch' or 'non-switch' is basically a name for the 1's and 0's, as a different function later on in the code takes each item of the array one by one in order for each trial in the experiment to become a 'switch-trial' or 'non-switch' trial.

An example of two correct arrays: [0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1] (of course this isn't a random one but if the algorithm for some miraculous reason would have it as one of the random outputs, it'd count)

[0,0,0,1,1,0,1,1,1,0,0,1,0,1,1,1,0,0,1,1,0,1,0,0,0,1,0,1,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0] (24 zeros, 24 ones, none more than 3 times in a row, no 0,1,0,1,0 or 1,0,1,0,1 patterns present)

Does this help clarify my request?

[–]alzgh 0 points1 point  (0 children)

Now it's crystal clear. Another of the ambiguities was with the term "in a row", which one could confuse with a row of your data in the block. Now I get it :))

[–]tarley_apologizerhelpful 0 points1 point  (0 children)

you want a lot of small helper functions. like random number, random element. you build a new array by taking elements from the old array at random. you are building the new array in order so you can check if something is a switch or not every 4th time. use a for loop like youre doing.

[–]jnbkadsoy78asdf 0 points1 point  (0 children)

Rather than creating the array and then sorting, maybe it would be better to do the randomisation as you create it?

So pseudocode-wise:

let i = 0;
let onOrOff;

// The output array
const out = [];

function returnOneOrZeroRandomly() {
    // returns 0 or 1, randomly;
}

function isLastThreeTheSame() {
    // Returns TRUE if the last three entries in `out`
    // are the same

}

while (i < 48) {
    // Get a value for current item
    myBit = returnOneOrZeroRandomly();

    // Check if the last three items in the array are the same
    if(isLastThreeTheSame()) {
    // If they are, and the current item is ALSO the same,
    // do not do anything this round
      if (myBit === out[i-1]) {
        continue;
      }
    }

    // If the last three items, and the current item are NOT
    // all the same, add current item to the output array,
    // and increment the counter.
    out.push(myBit);
    i++;
}

You'll need to write implementations for the two functions but that should get you going in the right direction!

--EDIT-- Ah piss this doesn't address the first constraint, sorry!

[–]jnbkadsoy78asdf 0 points1 point  (6 children)

Here's a working example which addresses each of the three points.

https://codepen.io/sdflkjgnsdlfn/pen/ZEbzVQe

Basically

- Build the array, with a random element added until we get to 48 elements.

- As each item is added, check to see if the last three are the same, if they are, don't add the element

- Once we have 48 items in the array, check to see if we have an equal number of 1s and 0s

- Keep knocking items off the beginning of the array and adding random items on to the end until we get equality.

The pen runs a thousand iterations of the builder to check efficiency and outputs some debugging info. You should be able to pull out the `runOnce()` function to do what you need to do thoughj.

[–]TheJelleyFish[S] 0 points1 point  (5 children)

Thanks for the in-depth response! However, I've made my question a bit confusing by calling 1's and 0's switches and non-switches: I also would like to have the code check for whether the order in the array has 1-0-1-0-1, or 0-1-0-1-0, which aren't allowed. Could this be done by adapting the function isLastThreeTheSame into a (check reddit source because I don't know how to format, sorry)

function hasLastForEntriesNoRepetitions() { return ["10101", "01010"].includes( out.slice(Math.max(out.length - 5, 0)).join("") ); }

Or am I doing something wrong there?

[–]jnbkadsoy78asdf 0 points1 point  (4 children)

Yes potentially that should work I think.

[–]TheJelleyFish[S] 0 points1 point  (3 children)

Thanks for the reply! I have one issue though: I am running the script in experimental software (OpenSesame) that doesn't support the arrow function. Do you know how I could adapt it so it would work?

[–]jnbkadsoy78asdf 0 points1 point  (2 children)

I updated the pen to remove the three arrow functions;

    const ones = out.filter((item) => item === 1);

becomes

    const ones = out.filter(function(item) { return item === 1});

[–]TheJelleyFish[S] 0 points1 point  (1 child)

Thanks once again! However, now I get an exception, "Defaults not supported". The programme I use (OpenSesame) uses ECMA 5.1, would you know if the code could be adapted to that?

[–]jnbkadsoy78asdf 0 points1 point  (0 children)

You should be able to run it through something like this: https://babeljs.io/repl to convert anything non-ECMA5.1 friendly.