Selecting random rows and repeating without replacement

OS (e.g. Win10): MacOS sierra
PsychoPy version (e.g. 1.84.x): 1.84.2

What are you trying to achieve?:
I have an outer loop randomly selecting 20 conditions out of 40 (cues, $random(20)*39) and an inner loop that selects 3 out of 6 conditions(targets) from the 20 conditions (cues) selected from the outer loop.

I need the inner loop to repeat the items (targets) it has selected 3 times. I thought that using “random” for the method would mean that selection is done without replacement. However with every cycle (choosing 3 out of 6, $random(3)*5) I seem to be getting repeats of the same item.

Is the “random” without replacement only applicable if its running through the whole list? Is there a way I can let psychopy know to select without replacement?

I’m not sure what you mean by “random with replacement”?

Hi! I used random(3)*5 so that I can randomly select 3 rows from the 6 available but sometimes i get repeats of the same row. e.g: a row of a,b,c,d,e,f i got a,b,b or c,d,c. So apparently, it was due to the rounding off of the random value for e.g. [0.11, 0.12, 0.31) will become [0.1,0.1,0.3] which resulted in me getting repeats within one round of randomisation. so that is solved already.

I do have another question though, I want to present the selected rows randomly without being tied to the same first cues. So what i have now is cueA - target A, Cue A- target C, Cue A- target D followed by Cue F - target B, Cue F - target E , Cue F - target A , the same cues are presented together.

So I was wondering how I can get the inner and outer loops to run first (60 word pairs) and assigned to a variable to be shuffled before being presented and repeated thrice?

Hi,
PsychoPy Builder uses the random functions provided by the numpy library (beware: these are not the random functions provided by the Python standard library itself). This is the relevant line you’ll see that the start of all Builder-generated scripts:

from numpy.random import random, randint, normal, shuffle

It’s important that you understand what these functions do before using them. e.g. look online for the numpy docs:
https://docs.scipy.org/doc/numpy/reference/routines.random.html

You’ll see that numpy.random() returns “random floats in the half-open interval [0.0, 1.0)”. If you specify a size (as you do), then you get an array of random floating point numbers. Each of those numbers is independent of the others. And because you are sampling from a continuous distribution, the notion of “sampling without replacement” doesn’t even make sense, as there are effectively an infinite number of floating point numbers to choose from.
But note that even if you were directly sampling from integers, you would still have the same problem (it’s not the rounding that is causing your issue, it is that these numpy sampling functions inherently return values that are independent of each other. So note that “that is solved already” is not true.

Two things to take from this:

(1) Be careful to use the appropriate functions. If you want a sample of random integers, use the randint() function rather than random(). e.g. randint(low=0, high=39, size=20)

(2) The above still doesn’t give you sampling without replacement, as each number is still independent of the others (i.e. repeated values are possible). In programming, “sampling without replacement” is more usefully thought of as:
– creating a finite set (e.g. the numbers 0 to 39)
– shuffling them to create a random order
– taking a subset of the first (or last, or whatever) n values.

e.g.

# create initial list once:
row_numbers = range(40) 

# do this each time a new sample without replacement is needed:
shuffle(row_numbers) 
row_sample = row_numbers[0:20]
2 Likes

Hi
Thank you very much for the explanation! This is all new to me so your explanation really helped a lot!

Hi there, I’m having a similar problem where LoopType is set to random and nReps is set to 1, but I am still seeing repeat indices. Reading this, I can see how my problem might also have to do with the rounding off of the random value. Could you explain to me how you checked this and made the fix?

Please show us what you are doing to select your rows (i.e. a screenshot of your loop dialog, any custom code snippets).

Hello, does anyone know how to make random selections that export to html for online use? I think the $random(20)*39 and such brings down the whole Builder upon trying to convert to html, whereas it runs fine locally :frowning: