NumpyRNGContext#

class astropy.utils.misc.NumpyRNGContext(seed)[source]#

Bases: object

A context manager (for use with the with statement) that will seed the numpy random number generator (RNG) to a specific value, and then restore the RNG state back to whatever it was before.

This is primarily intended for use in the astropy testing suit, but it may be useful in ensuring reproducibility of Monte Carlo simulations in a science context.

Parameters:
seedint

The value to use to seed the numpy RNG

Examples

A typical use case might be:

with NumpyRNGContext(<some seed value you pick>):
    from numpy import random

    randarr = random.randn(100)
    ... run your test using `randarr` ...

#Any code using numpy.random at this indent level will act just as it
#would have if it had been before the with statement - e.g. whatever
#the default seed is.