Create random default (initial value) in formfields, save initial value and use it in a function

270 views
Skip to first unread message

Monique Meyer

unread,
May 21, 2021, 4:27:46 AM5/21/21
to oTree help & discussion

Hey all,

i want to set a default value (initial value) in formfields that is randomly drawn according to a special probability function. [with a certain probability (p) it is a constant and with a probability (1-p) it is randomly drawn from a normal distribution in the interval (a,b)].

I want to save the default value (initial value) to use it in my statistical analysis later on. I addition, want to program an audit rule that depends on the initial value. Therefore, I have to use the initial value in a function later on.

I read in the otree documentation [About using random()] that I cannot programm a random constant. Instead I have to programm a function. Now, I am totally confused because I really cannot figure out how to solve my problem!

Hopefully, somebody can help me to resolve my confusion. Many thanks!

Best,

Monique



Monique Meyer

unread,
May 21, 2021, 8:38:03 AM5/21/21
to oTree help & discussion
Bild1.png
This is what I tried so far. I would call I a nice try...

Chris @ oTree

unread,
May 21, 2021, 10:39:41 AM5/21/21
to oTree help & discussion
Try this:

class Subsession(BaseSubsession):
    pass


def creating_session(subsession: Subsession):
    import random

    for player in subsession.get_players():
        r = random.randint(1, 10)
        player.default = r
        player.item_1 = r


class Group(BaseGroup):
    pass


class Player(BasePlayer):
    default = models.FloatField()
    item_1 = models.FloatField()


Reply all
Reply to author
Forward
0 new messages