#1 by ldnlab
Hi all,
I'm running a multiplayer study on Prolific, and I've noticed a problem with random.shuffle. Somehow, the arrays that I randomize keep the same order for every session I run for an entire day. The arrays should randomize for each session I run, but if I run 5 or 6 sessions in a day, they all have the same exact "randomized" order.
class C(BaseConstants):
NAME_IN_URL = 'multiplayer_app'
PLAYERS_PER_GROUP = 3
NUM_ROUNDS = 150
colors = ["red", "blue", "green", "yellow", "purple", "brown"]
random.shuffle(colors)
for example..
say session 1 returns: colors = ["blue", "yellow", "red", "brown", "purple", "green"],
all other sessions I run that day (which are independent from this one) return the same exact 'randomized' sequence of colors.
Can anybody explain why this is, and a possible solution? Thanks.
#2
by
Furuneko
Hi,
To implement a new randomisation in each session:
- Remove from class C() the line:
random.shuffle(colors)
- Define the following global function (outside of any class in your __init__.py file):
def creating_session(subsession: Subsession):
if subsession.round_number == 1:
session = subsession
session.vars['colors'] = random.sample(C.colors, k=len(C.colors))
- Where you need it, retrieve the randomised list of colors using:
session = subsession
colors = session.vars['colors']
Best wishes
#3 by ldnlab
Thanks for the help. I am now getting "NameError: name 'subsession' is not defined" at the "session = subsession" line where I'm retrieving the list. Any ideas on why that is? I've never used subsession variables so I'm not sure where the issue is.
#4
by
Furuneko
I assume that you are using a text editor (most probably, you would benefit enormously from using oTree Studio instead):
- I left a typo, the line should read:
session = subsession.session
- You would need to make sure that the function creating_session() is palced below the lines where the class Subsession() is defined.
#5 by ldnlab (edited )
I've tried session = subsession.session, and I'm still getting the error. And yes, creating_session() is below Subsession().
This is how I'm trying to access:
class Fixation(Page):
form_model = 'player'
timeout_seconds = 1
@staticmethod
def vars_for_template(player: Player):
group = player.group
session = subsession.session
colors = session.vars['colors']
#6
by
Furuneko
Apologies, I didn’t properly read your second post: - when retrieving the session object, you need to start from player, group, or subsession; - if you are in a method/function where you have access to the player object, as it is often the case, then you would do: session = player.session
#7 by ldnlab
Ahh yes now it works. Thank you so much for your quick and extremely helpful responses!