Determine Role by Random 2

387 views
Skip to first unread message

Zhang Sr

unread,
Sep 17, 2021, 11:46:20 AM9/17/21
to oTree help & discussion
Hi guys,

Earlier this week I posted a question regarding the random mechanism, it hasn't got fixed yet. Here is the updated idea:

* I need ONE leader and FOUR followers per group. 

1. on the player page:

def roles (player):
import random
random_value = random.sample(range(0,5), 5)
if player.random_value == 1:
   return  'Leader'
else:
   return  'follower'

roles = models.StringField()
random_value = models.IntergerField()

But it doesn't work, I tried to get player.random_value, it is none, so I think at the first step, the "random value" is not given to the "players". So where should I "import random"?

Or anyone have any better idea to assign the role by random mechanism? I think the built in function assign by id, which is not that random as I wanted.

Thank you so much!
Shaorong

Chris @ oTree

unread,
Sep 17, 2021, 3:38:33 PM9/17/21
to oTree help & discussion
I pasted that code into a function in oTree Studio and when I try to download the code I get this error: "local variable 'random_value' is assigned to but never used".

the first line should be player.random_value = random.sample(... otherwise you have a local variable 'random_value' that is never used

instead of this function, i would suggest to check if player.id_in_group == 1. if so, show the leader page, etc. and you can use subsession.group_randomly, group.set_players, etc, to randomly shuffle id_in_group.

Zhang Sr

unread,
Sep 20, 2021, 2:52:38 AM9/20/21
to oTree help & discussion
Hi Chris, thank you for the suggestion. To my understanding, using subsession.group_randomly will end up with players in different groups, but I need the players in the same group during the whole game. Am I misunderstand " group.randomly()"? Can I get a clearer statement about the subsession.group_randomly?

I actually tried player.random_value, but also end up with the "none value", I think it is because random_value = random.sample(range(0,5), 5) will get a list of 5 numbers, but player.random_value is not choose one from them. 

Sorry for any inconvenience and thank you in advance.
Shaorong

sdl...@gmail.com

unread,
Feb 15, 2022, 5:45:22 PM2/15/22
to oTree help & discussion
Reply all
Reply to author
Forward
0 new messages