URL of experiment: bsl test [PsychoPy]
Description of the problem: We want the experiment to randomly choose an emotion out of six options when given a set of probabilities to act as weights. It worked fine on builder using random.choices but it will not work online, we got the following error message:
- TypeError: Math.random.choice is not a function
We then learned that import does not work for online experiments so we tried to write our own function in builder to do the same thing as Python’s random.choices function, but it will not run. Function code below:
var randomChoices;
randomChoices = function (population, weights = null, size = 1) {
let choices = ;
if (weights === null) {
// If no weights are provided, choose uniformly at random
for (let i = 0; i < size; i++) {
let choice_index = Math.floor(Math.random() * population.length);
choices.push(population[choice_index]);
}
} else {
// If weights are provided
let cumulativeWeights = weights.map((sum => value => sum += value)(0));
let totalWeight = cumulativeWeights[cumulativeWeights.length - 1];
for (let i = 0; i < size; i++) {
let randomWeight = Math.random() * totalWeight;
let choice_index = cumulativeWeights.findIndex(cumulativeWeight => cumulativeWeight >= randomWeight);
choices.push(population[choice_index]);
}
}
return choices;
}
Thankful for any help! Let me know if more information is needed.