Randomizing page sequence...

1,310 views
Skip to first unread message

Seung Kyo Ahn

unread,
Feb 22, 2021, 9:42:11 PM2/22/21
to oTree help & discussion
Hello Chris and all,

Sorry for the strings of questions... 

I am looking for a way to randomize page sequence - just two pages among 21 pages that I have. I looked at the snippets on the topic. If I am understanding correctly, the snippets seem to be on randomizing different apps, not the pages within the same app (please correct me if I am wrong).

If I can, I would appreciate any of your tips/knowledge on this.
Thanks!


Ted



@Chris - FYI, the demo page on the Snippets do not seem to be working. I am not sure if its intended or if I went into the wrong flow, but just letting you know.

Here is the error I got:
Screen Shot 2021-02-22 at 8.37.42 PM.png

Thanks!



Ted

Chris @ oTree

unread,
Feb 26, 2021, 6:07:52 PM2/26/21
to oTree help & discussion
If it's just 2 pages, then that's pretty straightforward. Let's say you have PageA and PageB. Define a player field called 'page_A_first'. Then randomize players to the condition:

def creating_session(subsession):
  import random
  for p in subsession.get_players():
    p.page_1_first = random.choice([True, False])

Then, make 2 copies of PageA, and make sure players only see one of the two:

class PageA(Page):
  def is_displayed(player):
    return player.page_A_first

class PageB(Page):
  pass

class PageA_Copy(Page):
  def is_displayed(player):
    return not player.page_A_first


Daniel Frey

unread,
Mar 4, 2021, 5:55:57 AM3/4/21
to oTree help & discussion
A method for randomizing more than 2 pages works like this (here for three tasks):

class Constants(BaseConstants):
  name_in_url = 'random_page_order'
  players_per_group = None
  tasks = ['A', 'B', 'C']
  num_rounds = len(tasks)

def creating_session(subsession):
  import random
  if subsession.round_number == 1:
    for p in subsession.get_players():
      round_numbers = list(range(1, Constants.num_rounds+1))
      random.shuffle(round_numbers)
      print(round_numbers)
      p.participant.vars['task_rounds'] = dict(zip(Constants.tasks, round_numbers))
      print(p.participant.vars['task_rounds'])

class TaskA(Page):
  def is_displayed(player):
    return self.round_number == player.participant.vars['task_rounds']['A']

class TaskB(Page):
  def is_displayed(player):
    return self.round_number == player.participant.vars['task_rounds']['B']

class TaskC(Page):
  def is_displayed(player):
    return self.round_number == player.participant.vars['task_rounds']['C']

page_sequence = [TaskA, TaskB, TaskC]

The print-commands are not necessary, but can help to understand how the randomization works.

Seung Kyo Ahn

unread,
Mar 13, 2021, 10:33:13 PM3/13/21
to oTree help & discussion
Sorry for replying to you guys a bit late. I was on a different project and finally got back to this one!

Thank you so much guys - I will try them out!



Ted

2021년 3월 4일 목요일 오전 4시 55분 57초 UTC-6에 danie...@iop.unibe.ch님이 작성:

Seung Kyo Ahn

unread,
Mar 14, 2021, 1:40:55 PM3/14/21
to oTree help & discussion
Hello Chris,

Again, thanks for your response. I have been trying to use your code to randomize two pages, but I think I have been failing so far. The page that I am trying to randomize are PEQP2A (the copy of the page is PEQP2B) and PEQ3. However, the pages do not randomize but only PEQP2A shows up first. 

I think I did exactly what you told me, but could you please tell me if I am missing anything? I will list how I defined each subsession and page below. 

Subsession field:
Screen Shot 2021-03-14 at 12.35.23 PM.png

Player field:
Screen Shot 2021-03-14 at 12.36.21 PM.png

Page PEQP2A:
Screen Shot 2021-03-14 at 12.37.15 PM.png

PEQP3:
Nothing (as it's a pass)

PEQP2B:
Screen Shot 2021-03-14 at 12.38.05 PM.png


Thank you in advance for your assistance,



Ted

2021년 2월 26일 금요일 오후 5시 7분 52초 UTC-6에 Chris @ oTree님이 작성:
Reply all
Reply to author
Forward
0 new messages