Random Number Generators

Usually when dealing with stochastic processes, there comes a point where you want to set the seed used by the Random Number Generator (RNG). This may be for reproducing results by a colleague, forcing a deterministic behavior when debugging a code etc.

CoopIHC supports setting seeds for the following two sources:

  • The sample() method of Space.

  • Other calls to a RNG, e.g., when implementing a stochastic transition function.

Seeding sample()

To set the seed for all spaces, you can directly pass a seed keyword argument to Bundle:

 1example_task = ExampleTask()
 2example_user = ExampleUser()
 3example_assistant = ExampleAssistant()
 4bundle = Bundle(
 5    task=example_task, user=example_user, assistant=example_assistant, seed=1234
 6)
 7bundle.reset()
 8# >>> print(bundle.state)
 9# ----------------  -----------  -  -------------------------------------
10# game_info         turn_index   0  CatSet(4) - int8
11#                   round_index  0  Numeric() - int64
12# task_state        x            0  Numeric() - int64 seed:1234(0, 0)
13# user_state        goal         4  Numeric() - int64 seed:1234(1, 0, 0)
14# user_action       action       0  Numeric() - int64 seed:1234(1, 1, 0)
15# assistant_action  action       0  CatSet(1) - int64 seed:1234(2, 1, 0))
16# ----------------  -----------  -  -------------------------------------
17

Note

You can set seeds manually, by calling an object’s _set_seed() method, or by passing a seedsequence keyword argument to the object’s constructor. However, setting seeds via the bundle is the preferred way, because it ensures a very low risk of RNG collisions between the various spaces, see NumPy’s documentation for parallel RNG

Calling a RNG from within CoopIHC code

You can call an RNG, which will use a spawn from the same seed that was used during the bundle initialization, by calling a CoopIHC component’s _get_rng() method.

# Inside a *CoopIHC* component
# generate a random number
self.get_rng().random()