I know it’s not a new issue and has to do with the Java code (since it worked without it), but I cannot figure out where my mistake is. I am using the latest version of PsychPy and have Auto->JS version.
Please find the few lines of code I used and please help me find my problem.
I’m not sure whether this is the reason for this specific error, but there’s no numpy in javascript, so you won’t be able to import it outside of the python version of your experiment.
If you need a function that does something similar to random.choice in numpy, here is a simple one I use in a couple of my experiments:
function sample(target, nSamples){
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
var tmp = [];
while (tmp.length < nSamples){
var x = Math.floor(getRandomArbitrary(0, target.length));
if (!tmp.includes(x)){
tmp.push(x);
}
}
return(subset(target, tmp));
}
It relies on this subset function to work, so you would need to include this too:
function subset(target, elements){
var i;
var tmp = [];
for (i=0; i < elements.length; i++){
tmp.push(target[elements[i]]);
}
return(tmp);
}
(I am far from a javascript expert, so there may be much simpler ways to accomplish the same thing, but this is good enough for my purposes.)
Thank you ever so much for your quick reply. Yes, I do need this function, because I have to shuffle between blocks or actually 4 different files. So it is very very essential.
I am very bad at programming so please could you explain where I could paste this code?
I want to run the experiment both off- and online.
thank you for your suggestion. The thing is, I do not only need the shuffle, but also choise.
My loop is a bit complicated:
I have two kinds of stories, priv and pub (10 each) and two kinds of sentences priv and pub (60 each)
They should belong to each other as to the quality pub or priv. Then I have a catch task with a separate file.
I want to present 1 story, 6 belonging sentences, math task, 1 story, 6 sentences etc…So wile only choosing 6 random out of 60, I need to shuffle the stories (and thus, the qualitues)
In principle could you shuffle the numbers 0 to 59 and then create a new array from the first 10 entries?
I’m on my phone doing a puzzle with my 2 year old so a little distracted.
What I do for something similar is have loops loading items into arrays and then have my trials loop taking items from these arrays instead of the excel files.
I am very new to PsychPy so I exuse my naive questions. Do you mean creating something like a dictionary? I am not sure how I can do it.
Sorry for bothering
I works fine offline but on pavlovia it returns an error: Range is not defined
I guess it is because I have to define this variable in JS but I have no idea how. This offline-online conversion seems to be quite tricky still, in spite of the automatic Python-JS translation.
If you address items in the array using a variable which you increment manually rather than trials.thisN then you would get the first 10 items the first time and the second 10 items the second time, etc.
Use different variables which increment at different rates to address your different arrays.
I seems to be working on the desktop, but NOT only. So it just continues showing all the 60 items.
And sometimes I get an error ONLINE: MyCount1 is not defined.
If you address items in the array using a variable which you increment manually rather than trials.thisN then you would get the first 10 items the first time and the second 10 items the second time, etc.
Use different variables which increment at different rates to address your different arrays.
Could you expand on that, please? The counter picks the same items every other run. Many thanks!