Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

shuffle images from pool

edited June 2021 in OSWeb

Hi everybody,

I am planning a memory experiment in which subjects are presented with lists of words when each word should appear on a different background - a picture of a forest or a picture of a sea.

I uploaded the images to the pool but I can not pull them from the pool and create a randomization between them. When I run the experiment I get no error but the words appear on a black background and not on the background of the images.

I do not know the software well and I mostly do not know JS codes.

I used these lines:


function randomArrayShuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
var backround=["sea", "forest", "sea", "forest", "sea", "forest","sea", "forest", "sea", "forest", "sea", "forest", "sea", "forest", "sea", "forest"];
randomArrayShuffle(backround);
backround.pop();


Thank you.

Zohar

Comments

  • Hi Zohar,

    I am not an expert on JS, but to see whether there actually is an error message, you can check the console of the browser in which you are test running your code. Do you know how to access it?

    In any case, assuming that there is indeed nothing wrong with your script, it would mean that not the randomization is flawed but the drawing part. How did you implement that? You can upload your experiment if you wish (incl. some of the images).

    Eduard

    Ps. I believe in your code, you can't just pop the background, you need to capture it in a variable of vars

    For example something like this:

    vars.background = backround.pop()

    Buy Me A Coffee

  • FabFab
    edited August 2021

    Hi @zohar ,

    Your code works but you're probably not passing the information to the picture object on your sketchpad. But I'd modify the end:

    function randomArrayShuffle(array) {
    var currentIndex = array.length, temporaryValue, randomIndex;
    while (0 !== currentIndex) {
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
    }
    return array;
    }
    var backround=["sea", "forest", "sea", "forest", "sea", "forest","sea", "forest", "sea", "forest", "sea", "forest", "sea", "forest", "sea", "forest"];
    vars.newarray = randomArrayShuffle(backround);
    console.log(newarray)
    

    This line stores the shuffled array into a variable you'll be able to access from the trial sequence:

    vars.newarray = randomArrayShuffle(backround);
    

    If you add the following line at the end of your script, you'll see the shuffled list in the console to check that it is indeed shuffled:

    console.log(newarray)
    

    I think the issue may be with the structure of your task or the lack of attribution of the content of the shuffled array to a variable that your task actually uses to retrieve the picture in the pool.

    A couple of clarifications first...

    (1) I'm going to assume that you want to randomize the order of the background pictures independently of the randomization of the trials in your loop. If you weren't, then I'd question the rationale for using the code at all (since you'd just have to set the order of the loop to "random").

    (2) Just to be on same page, let's clarify that that code must be executed ahead of a block of trials (since you have 10 elements in your background array, I'm assuming that your loop contains 10 trials). Your use of the the pop function in that code makes me think you're actually truing to use the code on every trial...

    With these out of the way, let's address the key issue. I think that the reason your screen remains black is because shuffling an array does nothing else than shuffling an array. That is, it does not show pictures. To show pictures, you'd have to have a sketchpad in your trial sequence with an actual picture object. Then the whole issue becomes what picture file you set that object to display on every trial. You'd want that information to be pulled from a column in your loop.

    So I think that what you're missing is the bit where you scan through the shuffled array, as the loop runs, to tell the task to use and nth element of the array for the nth trial in the loop. That's where you'd use the vars.newarray and select the elements one by one (taking the 1st + ".jpg" in the 1st trial, the 2nd + ".jpg" for the 2nd trial etc.).

    Have a look at the solution published here. I think it should help.

    Note that in that solution, I do not alter the shuffled array, I just navigate through it as trials go. So, I'm not using the pop function as you do in your code (that is destructive as is eliminates the last element of the array). The fact that you have it in that code makes me think that you've placed the code in the trial sequence instead of before the loop. It would make little sense to build the array and eliminate the last element before running the loop. And it makes little sense to recreate a new shuffled array on every trial to just use the last element with the pop function. So, in a nutshell, the shuffling must be done once and before the loop. No "pop" at that stage. Then, in the trial sequence, you can use the pop function to select the last element of the array (so that trial by trial, your array will shrink to nothing). In the example above, I have not used this destructive method. Instead, I leave the array intact and count the trials to go pick the elements in turn.

    Good luck!

    Fabrice.


    _____________________________________________________________________________________

    If you found my reply helpful and wish to invite me to a coffee, you can do so here 😉: https://www.buymeacoffee.com/psyfab

    Buy Me A Coffee

Sign In or Register to comment.