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