Pavlovia: How to implement the range function?

Yes, so random.choice doesn’t exist in JS. There are two solutions to this (I recommend the second but I will outline both for clarity:

  1. Try adding a JS function that performs a similar function to random.choice in JS.
function choose(choices) {
  var index = Math.floor(Math.random() * choices.length);
  return choices[index];
}

then in any cases where you have used ‘random.choice’ in code components, change code type to ‘both’ and replace ‘random.choice’ with ‘choose’

  1. Use shuffle (outlined on crib sheet) and then to randomly select call something like:
myList = [1, 2, 3]
shuffle(myList)
myPick = myList[0]

I recommend the second because then you can keep later code components as code type auto->JS (which is nicer in the long run).

Hope this helps,
Becca

2 Likes