# Randomization code not working online

**URL:** <https://discourse.psychopy.org/t/randomization-code-not-working-online/32796>\
**Category:** Online experiments\
**Created:** [January 16, 2023, 4:39am UTC](https://discourse.psychopy.org/t/randomization-code-not-working-online/32796 "2023-01-16T04:39:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bennett\_K.N](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/bennett_k.n/32/34734_2.png) [@Bennett\_K.N](https://discourse.psychopy.org/u/Bennett_K.N)\
**Post date:** [January 16, 2023, 4:39am UTC](https://discourse.psychopy.org/t/randomization-code-not-working-online/32796/1 "2023-01-16T04:39:56Z")

</div>

I have some code that I found on here that basically allows me to take two columns in an excel sheet, randomize them in a list, and then display the images contained within the rows, so that psychopy randomly takes an image from one column or the other. It is working offline, but it is not working online. If anyone can help me I would really appreciate it! Here is the python and javascript code:

**Begin Experiment:**

_Python code_

cue\_list = [‘Image’, ‘blank’] \* 31  
shuffle(cue\_list)

_JS Code try #1_  
cue\_list = ([“Image”, “blank”] \* 31);  
util.shuffle(cue\_list);

**Begin Routine:**

_Python code_

if cue\_list:  
cue = cue\_list.pop()  
thisExp.addData(‘cue\_type’, cue)  
cue = eval(cue)

_JS Code try #1_  
if (cue\_list.length \> 0) {  
let cue = cue\_list.pop();  
psychoJS.experiment.addData(‘cue\_type’, cue);  
cue = eval(cue);  
}

_JS Code try #2_  
if (cue\_list) {  
cue = cue\_list.pop();  
psychoJS.experiment.addData(“cue\_type”, cue);  
cue = eval(cue);  
}

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [January 16, 2023, 10:16am UTC](https://discourse.psychopy.org/t/randomization-code-not-working-online/32796/2 "2023-01-16T10:16:49Z")

</div>

Hello,

see the [crib sheet](https://docs.google.com/document/d/183xmwDgSbnJZHMGf3yWpieV9Bx8y7fOCm3QKkMOOXFQ/edit) by wakecarter how to modify your JS-part of the code. Auto-translated doesn’t work here. For example:

```auto
[0, 1] * 10

```

has to be translated (by hand) to

```auto
Array(10).fill([0,1]).flat();

```

Best wishes Jens

---

<div class="post-metadata">

**Author:** ![Bennett\_K.N](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/bennett_k.n/32/34734_2.png) [@Bennett\_K.N](https://discourse.psychopy.org/u/Bennett_K.N)\
**Post date:** [January 17, 2023, 4:23am UTC](https://discourse.psychopy.org/t/randomization-code-not-working-online/32796/3 "2023-01-17T04:23:06Z")

</div>

Thank you! I’ve gone through the crib sheet, and I have changed everything it suggested (code below) but am still stuck.

Begin Experiment:  
cue\_list = Array(31).fill([“Image”, “blank”]).flat;  
util.shuffle(cue\_list);

Begin Routine:  
if (cue\_list) {  
cue = cue\_list[0];  
psychoJS.experiment.addData(“cue\_type”, cue);  
cue = window[cue];  
}

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [January 17, 2023, 8:47am UTC](https://discourse.psychopy.org/t/randomization-code-not-working-online/32796/4 "2023-01-17T08:47:01Z")

</div>

Hello Bennett

> [@Bennett\_K.N](#):
>
> but am still stuck.

you provide insufficient information.

Also, when posting code surround it with triple `to get properly formatted code.

Best wishes Jens

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [January 17, 2023, 12:25pm UTC](https://discourse.psychopy.org/t/randomization-code-not-working-online/32796/5 "2023-01-17T12:25:48Z")

</div>

> [@Bennett\_K.N](#):
>
> Begin Routine:  
> if (cue\_list) {  
> cue = cue\_list[0];  
> psychoJS.experiment.addData(“cue\_type”, cue);  
> cue = window[cue];  
> }

Try

```auto
if (cue_list) {
cue = cue_list.pop();
psychoJS.experiment.addData(“cue_type”, cue);
}

```

I assume that if(cue\_list) is to prevent an error when the cue\_list becomes empty.

What does cue = window[cue]; do? You had cue = eval(cue); before
