rlberry.seeding.seeder.Seeder

class rlberry.seeding.seeder.Seeder(seed_seq=None, spawn_seed_seq=True)[source]

Bases: object

Base class to define objects that use random number generators.

See also: https://numpy.org/doc/stable/reference/random/bit_generators/generated/numpy.random.SeedSequence.html

Parameters:
seed_seqnp.random.SeedSequence, rlberry.seeding.Seeder or int, defaultNone

Seed sequence from which to spawn the random number generator. If None, generate random seed. If int, use as entropy for SeedSequence. If seeder, use seeder.seed_seq

spawn_seed_seqbool, defaultTrue

If True, uses seed_seq to spawn a new seed sequence (strongly recommended) for the Seeder. If False, uses the input seed_seq to define the Seeder. Warning: Setting to false can lead to unexpected behavior. This argument is only used internally in rlberry, in Seeder.spawn(), to avoid unnecessary spawning.

Attributes:
rngnumpy.random._generator.Generator

random number generator provided by rlberry.seeding

Examples

>>> from rlberry.seeding import Seeder
>>> from rlberry.envs import gym_make
>>> from rlberry.agents import RSUCBVIAgent
>>>
>>> seeder = Seeder(123)
>>> seeder.rng.integers(5)
>>>
>>> env = gym_make('MountainCar-v0')
>>> env.reseed(seeder)
>>> agent = RSUCBVIAgent(env)
>>> agent.reseed(seeder)

Methods

reseed([seed_seq])

Get new random number generator.

spawn([n, squeeze])

Spawn a list of Seeder from the seed sequence of the object (self.seed_seq).

generate_state

reseed(seed_seq=None)[source]

Get new random number generator.

Parameters:
seed_seqnp.random.SeedSequence, rlberry.seeding.Seeder or int, defaultNone

Seed sequence from which to spawn the random number generator. If None, generate random seed. If int, use as entropy for SeedSequence. If seeder, use seeder.seed_seq

spawn(n=1, squeeze=True)[source]

Spawn a list of Seeder from the seed sequence of the object (self.seed_seq).

Parameters:
nint

Number of seed sequences to spawn

squeezebool

If False, returns a list even if n=1. Otherwise, returns a Seeder if n=1.

Returns
——-
seed_seqlist

List of spawned seed sequences, or a single Seeder if n=1.