oTree Forum >

Generating a Random Number

#1 by ChelMill

Hi all, 

I am setting up a first price auction where the players' private value is generated randomly from a uniform distribution between two numbers. I am using Otree studio and set the following in the player models section under def ... and when I try to run it, it says that PRIVATE_VALUE is null. 

Am I missing a step or something? 


import random

from otree.api import Currency as cu
player.PRIVATE_VALUE = cu(random.randint(0, C.INITIAL_ENDOWMENT))

#2 by Chris_oTree

Although you defined a function, does this function get called from somewhere? There may be a warning about this in the sidebar in oTree Studio.

I recommend to use creating_session (Subsession function) because it gets executed automatically without you having to call it from somewhere.

#3 by ChelMill

Thank you so much. 

I used the creating session approach and it worked. I was missing the line of code where I actually used the variable and corrected to this:  

for p in subsession.get_players():
    import random
    
    from otree.api import Currency as cu
    PRIVATE_VALUE = cu(random.randint(0, C.INITIAL_ENDOWMENT))
    p.PRIVATE_VALUE = PRIVATE_VALUE

Write a reply

Set forum username