How to use shuffle and choice from random function in an online experiment?

Description of the problem:
Hello,everyone.
I am writing an experiment program in psychopy’s builder mode. In the code component of our experiment program, I need to use the shuffle and choice functions from the random or numpy library to help me shuffle a list or randomly select an element from a list. When I compiled the experimental program into js scripts and uploaded them to the online experimental platform, they often failed to run properly online because they could not recognize numpy or import other libraries. Since I know almost nothing about js language, I would like to know how to implement the functions of shuffle and choice similar to Pyhton in online experiments?
Best regards!

Here is some of the code in my code component:

Hello,
Instead of using random.shuffle(), you can simply use shuffle(). It will simply translate itself to util.shuffle(); which is a correct randomization function that works in PsychoJs.

Let me know if it works for you.
Chen

Thanks for your useful advice, the shuffle function can work properly now, but I came across another question, it said “TypeError: 5 is read-only”.
Is it resulted by the list.remove?


Your creation of typelist isn’t doing what you want online. I would recommend

typeList = []
for Idx in range(32):
     typeList.append(1)
     typeList.append(0)
shuffle(typeList)

Hi, thank for your reply, but the error still exists :sob:

Make sure you are running the latest version by using Ctrl-Shift-R or an incognito tab

Thanks a lot for your advice!
I found that the problem was that the js language couldn’t combine two arrays like python, so I used python append (which automatically converts to js push) to solve this problem, but I now have another problem, I have tried several js language methods to delete the specified element in the array,

However, there is no way to remove specified elements from a list like python’s remove function, including splice, filter, etc. filter removes the specified element from the array, but according to my online experiments, it removes not the element I gave it, but another element in the list, which is very confusing to me.

Could you please give me some suggestions on the code? I just want to be able to remove a specified element from an array in a js script in the same way that remove can do in python.

As shown in the code, I want to remove the tarcolor element from the colors list.

Hello Johnathan_Fu

first, better start a new topic once it has been marked as solved.

second, look into @wakecarter’s crib sheet

There is a suggestion how to resolve this problem.

Best wishes Jens

2 Likes

Thanks for your suggestions, it’s all right!