0

for a simple app I try to develop a function that distributes player to different sessions (or treatments). For that purpose, I use a in built "creating_session" function.

At first, every session 1 to 3 should contain in this example 4 participants. Therefore, from the 12 first participants, 4 should be in session 1, session 2 and session 3.

The next participants should be randomly distributed onto the treatments.

The code below is working. However, in the real application I will have more than ten treatments.

Therefore, I was wondering if there is a way that I can make the code easier or shorter.


    def creating_session(subsession):
        import random
        c1 = 0
        c2 = 0
        c3 = 0
    
        for player in subsession.get_players():
            for i in range(50):
                treatment_rest = random.choice([1, 2, 3])
    
                if treatment_rest == 1:
                    c1 = c1 + 1
                    if c1 <= 4:
                        treatment = 1
                        break
                    elif c2 <= 4 or c3 <= 4:
                        continue
                    else:
                        treatment = 1
    
    
                elif treatment_rest == 2:
                    c2 = c2 + 1
                    if c2 <= 4:
                        treatment = 2
                        break
                    elif c1 <= 4 or c3 <= 4:
                        continue
                    else:
                        treatment = 2
    
    
                elif treatment_rest == 3:
                    c3 = c3 + 1
                    if c3 <= 4:
                        treatment = 3
                        break
                    elif c1 <= 4 or c2 <= 4:
                        continue
                    else:
                        treatment = 3

1 Answers1

0

There are certainly many ways to achieve this. One of them is the following:

def creating_session(subsession):
    import random
    treatments = [1, 2, 3] * 4
    random.shuffle(treatments)

    for player in subsession.get_players():
        player.treatment = treatments.pop() if treatments else random.randint(1, 3)

Let me explain what's going on here:

  • First of all, a list of all treatments is created and multiplied by 4 to have each treatment exactly 4 times in the list: treatments = [1, 2, 3] * 4.

  • Then the list is shuffled to randomize the distribution of these treatments among the first 12 participants: random.shuffle(treatments).

  • And finally, in the player loop, it is checked whether there are any treatments left in the list. If so, a treatment is removed from the list and assigned to the player. If not, i.e. the 12 treatments from the list have been used up, a random treatment integer between 1 and 3 (both included) is assigned to the player: player.treatment = treatments.pop() if treatments else random.randint(1, 3).

    The last line of code uses python's ternary operator and is a shortcut for:

      if treatments:
          player.treatment = treatments.pop()
      else:
          player.treatment = random.randint(1, 3)
    

    This is not as short, but perhaps easier to read and understand.

Note: The above code assumes that a field named "treatment" has been defined in the player model:

class Player(BasePlayer):

    treatment = models.IntegerField()
davidlucius
  • 206
  • 2
  • 4