Random number generation
Random number generation in bilby
uses a global numpy
Generator
object in bilby.core.utils.random
. The recommended usage is
>>> from bilby.core.utils import random
>>> x = random.rng.uniform()
where rng
is a numpy
random generator. For more details about
numpy
random generators, see the
numpy
documentation.
Warning
The rng
object should not be imported directly as it will not be seeded
by calls to bilby.core.utils.random.seed()
.
The random number generation can be seeded using the
bilby.core.utils.random.seed()
function:
>>> from bilby.core.utils import random
>>> random.seed(1234)
Seeding samplers
The different samplers in bilby
have different ways of seeding the random number
generator that depend on each sampler’s implementation. As such, seeding the bilby
random number generator with bilby.core.utils.random.seed()
does not guarantee that the
sampler will be seeded.
If the interface for a sampler supports seeding, then specifying either the specific
keyword argument or an equivalent argument (seed
, sampling_seed
or random_seed
will be automatically translated to the appropriate keyword argument)
when calling run_sampler()
will seed the sampler’s random number generator.
For example:
>>> import bilby
>>> likelihood = ...
>>> prior = ...
>>> bilby.run_sampler(
likelihood=likelihood,
prior=prior,
sampler="dynesty",
seed=1234,
)
Note
Some sampler interfaces do not support seeding.