Specify stimulus parameters according to normal distribution in loop

I am experiencing a mental block on how to get Builder to vary a certain stimulus parameter randomly according to a pre-defined distribution.
Say I want to have 10 trials where I show a bar where the length of the bar is determined by a normal distribution with a certain mean and standard deviation, and I want to have different blocks where I vary the standard deviation (e.g. high v low variability).
How can I do that in builder?
I know I could hard-code some values in the conditions file, but if I wanted to calculate the values on the fly, how would I do that.
At the moment my thinking is to have a variable that is a an array defined like , e.g.

practiceVar = numpy.random.normal(loc=0,scale = 1,size = 5)

How could I then get a loop to cycle through practiceVar?

Bonus question: what would be the js translation of the above, so that I could make it work in Pavlovia?

Thank you !

You could simply have a loop around your routine with the bar that repeats 10 times and have “practiceVar = numpy.random.normal(loc=0,scale = 1,size = 5)” in a code component in the begin routine tab. This way the size of the bar should change for each trial.

As for the js translation I never worked with random number generator. The crib sheet (PsychoPy Python to Javascript crib sheet 2021 - Google Docs) mention to not use numpy but I am not sure what the right code is.

For online use I generate a list of 100 or so Z scores in Excel which I assign to a list and then shuffle to draw from.

Hello @wakecarter and @tandy. Sorry for the delay in acknowledging your help. I have been snowed in with exam marking etc and had no time to think about fun things like programming :frowning:
I think the Z score list idea might work well for online. Do you copy and paste the excel generated scores into the code or are you getting JS to read it it in each time (and if so, how)?


I think when I did this I generated the numbers in Excel, then pasted into PFE (programmer’s file editor) so I could replace \n newline characters with commas and easily paste it into a code component as a list.

However, there is a suggestion of a function you can use here:

// Standard Normal variate using Box-Muller transform.
function randn_bm() {
    var u = 0, v = 0;
    while(u === 0) u = Math.random(); //Converting [0,1) to (0,1)
    while(v === 0) v = Math.random();
    return Math.sqrt( -2.0 * Math.log( u ) ) * Math.cos( 2.0 * Math.PI * v );

Also mentioned here: