oTree Forum >

Random selection of images from a pool w/o replacement at the participant level

#1 by Juliana

Hi, 

In my experiment, I intend to randomly select a pool of 30 images out of a larger pool of 46, for each participant without replacement. Below is my code. When I run this with more than 1 participant, I get an error message that the sample exceed the total number of images available. It seems that the random selection of images without replacement is happening at the session level (and not at the participant level). How can I fix this?

Thank you.

My code in init.py:

def creating_session(subsession: Subsession):
    PicturePool = [i.split("/")[-1] for i in glob('_static/questions/*PNG')]
    if subsession.round_number == 1:
        for p in subsession.get_players():
            selected_img = random.sample(PicturePool, k=30)
            PicturePool = [e for e in PicturePool if e not in selected_img]

            print('player', p.id_in_subsession)
            print(selected_img)
            print(len(PicturePool))

            p.participant.images = selected_img

Write a reply

Set forum username