def rng(spec=None, *, legacy=False): """ Get a random number generator. This is similar to :func:`sklearn.utils.check_random_seed`, but it usually returns a :class:`numpy.random.Generator` instead. .. warning:: This method is deprecated. Use :func:`seedbank.numpy_rng` instead. Args: spec: The spec for this RNG. Can be any of the following types: * ``int`` * ``None`` * :class:`numpy.random.SeedSequence` * :class:`numpy.random.mtrand.RandomState` * :class:`numpy.random.Generator` legacy(bool): If ``True``, return :class:`numpy.random.mtrand.RandomState` instead of a new-style :class:`numpy.random.Generator`. Returns: numpy.random.Generator: A random number generator. """ warnings.warn('rng is deprecated, use seedbank.numpy_rng', DeprecationWarning) if legacy: return seedbank.numpy_random_state(spec) else: return seedbank.numpy_rng(spec)
def test_generator(): rng = numpy_rng() assert isinstance(rng, np.random.Generator)
def test_generator_convert_from_rs(): rng1 = np.random.RandomState() rng = numpy_rng(rng1) assert isinstance(rng, np.random.Generator)
def test_generator_passthrough(): rng1 = np.random.default_rng() rng = numpy_rng(rng1) assert isinstance(rng, np.random.Generator) assert rng is rng1
def test_generator_seed_seq(): seq = np.random.SeedSequence(42) rng = numpy_rng(seq) assert isinstance(rng, np.random.Generator)