oTree Forum >

Assign a random number to each group (constant through rounds)

#1 by NZaccaria

Hi all,

I am having issues in something that apparently seems simple, but it is not for me. I would like to assign a random number between 1 and 24 to each group, such that it stays fixed during all experiment. The code that I have so far draws a different number at each round, but I have not been able to fix it. The random number in the code is given by 'groups.indexed'. Maybe should I write it somewhere else and not in the creating_session function?

class Group(BaseGroup):
    id_player0 = models.IntegerField() #models.IntegerField()
    realized_avg_price = models.IntegerField()
    transformed_avg_price = models.FloatField()
    indexed = models.IntegerField()

def creating_session(session):

    for groups in session.get_groups():
        groups.indexed = random.randint(1, 24)
        if session.round_number == 1:
            actual_player0 = C.dataset[groups.indexed - 1]
            groups.session.vars['actual_player0'] = actual_player0
            groups.realized_avg_price = actual_player0[session.round_number-1]
            groups.transformed_avg_price = groups.realized_avg_price / C.guess_max

#2 by ccrabbe

Hi NZaccaria -

crreating_session is run once per subsession - not once per session.  So each round in your game is getting its own execution of creating_session (with its own random number calculated).

So to only 	calculate it once, I would only calculate it for round_number==1, and then assign the value of that to later rounds, sosomething like:

if subsession.round_number == 1:
    groups.indexed = random.randint(1,24)
    groups.indexed = groups.in_round(1).indexed

#3 by NZaccaria

Hi Chris,

Thank you so much! This fixed my problem. It was such an easy fix, but I could not see it. Thank you again!


Write a reply

Set forum username