oTree Forum >

Random selection of choice sets read from csv file for multiple players

#1 by yogu

Hello Otree-Community,

In my experiment, I would like to randomly extract 10 choice sets from the 40 choice sets read from the csv file (without any duplications) and present them to the players sequentially in each round.
Furthermore, I would like to randomize the position in which the options are presented.

Now I am having a problem when I run this program with multiple players.

When the code shown below is applied to multiple players, the presented choice set and the presented option is the same for all of the players.


```
def read_csv():
    from pathlib import Path
    import csv

    csvfile = Path(__file__).parent / "choice_set.csv"
    f = open(csvfile, encoding='utf-8-sig')
    rows = list(csv.DictReader(f))
    random.shuffle(rows)
    return rows


class C(BaseConstants):
    NAME_IN_URL = 'survey_selflot_20230710'
    PLAYERS_PER_GROUP = None
    QUESTIONS = read_csv()
    QUESTIONS = QUESTIONS[0:10]
    NUM_ROUNDS = len(QUESTIONS)

def creating_session(subsession: Subsession):
    current_question = C.QUESTIONS[subsession.round_number - 1]
    for p in subsession.get_players():                
        if subsession.round_number == 1:
            subsession.session.vars["random_order"] = random.choice(["low_medium_high", "high_medium_low"])

        if (subsession.round_number - 1) % 5 < 2.5:
            # Switching order every 5 trials
            if subsession.session.vars["random_order"] == "low_medium_high":
                order = ["L", "M", "H"]
            else:
                order = ["H", "M", "L"]
        else:
            # Switching order every 5 trials
            if subsession.session.vars["random_order"] == "low_medium_high":
                order = ["H", "M", "L"]
            else:
                order = ["L", "M", "H"]

        for player in subsession.get_players():
            player.row_left = order[0]
            player.row_center = order[1]
            player.row_right = order[2]

            # Randomize the position of a row and record it in a variable
            positions = random.sample([1, 2, 3], 3)
            player.position_Maximin = positions[0]
            player.position_Egalitarian = positions[1]
            player.position_Utilitarian = positions[2]
```


However, I would like to randomize the choice set and the position of the options presented to each player.

Thank you in advance.

#2 by BonnEconLab

You have a redundant for p/player in subsession.get_players(): in your code.

This should do the job:

def creating_session(subsession):

    current_question = C.QUESTIONS[subsession.round_number - 1]
    if subsession.round_number == 1:
        subsession.session.vars["random_order"] = random.choice(["low_medium_high", "high_medium_low"])
    if (subsession.round_number - 1) % 5 < 2.5:
        # Switching order every 5 trials
        if subsession.session.vars["random_order"] == "low_medium_high":
            order = ["L", "M", "H"]
        else:
            order = ["H", "M", "L"]
    else:
        # Switching order every 5 trials
        if subsession.session.vars["random_order"] == "low_medium_high":
            order = ["H", "M", "L"]
        else:
            order = ["L", "M", "H"]
    for player in subsession.get_players():
        player.row_left = order[0]
        player.row_center = order[1]
        player.row_right = order[2]
        # Randomize the position of a row and record it in a variable
        positions = random.sample([1, 2, 3], 3)
        player.position_Maximin = positions[0]
        player.position_Egalitarian = positions[1]
        player.position_Utilitarian = positions[2]
        # Print results for debugging
        #print("Player " + str(player.id_in_subsession) + " in subsession #" + str(subsession.round_number))
        #print(player.row_left)
        #print(player.row_center)
        #print(player.row_right)
        #print(player.position_Maximin)
        #print(player.position_Egalitarian)
        #print(player.position_Utilitarian)

#3 by yogu

Thank you very much for your suggestion.
Actually, however, to randomize the choice set presented to each player, I found that I have to use participant class.

```
def creating_session(subsession: Subsession):
    for p in subsession.get_players():
        participant = p.participant
        if subsession.round_number == 1:
            participant.lmh_order_part1 = random.choice(["low_medium_high", "high_medium_low"])
        if (subsession.round_number - 1) % 5 < 2.5:
            if participant.lmh_order_part1 == "low_medium_high":
                order = ["L", "M", "H"]
            else:
                order = ["H", "M", "L"]
        else:
            if participant.lmh_order_part1 == "low_medium_high":
                order = ["H", "M", "L"]
            else:
                order = ["L", "M", "H"]
        
        p.row_left = order[0]
        p.row_center = order[1]
        p.row_right = order[2]

Write a reply

Set forum username