This is a pre-release version (2.5.0.dev0+git20251130.2de293a).Go to latest (2.4.4)
{ } Raw JSON

bundles / numpy 2.5.0.dev0+git20251130.2de293a / numpy / random / default_rng

cython_function_or_method

numpy.random:default_rng

Signature

def default_rng(seed = None)

Summary

Construct a new Generator with the default BitGenerator (PCG64).

Parameters

seed : {None, int, array_like[ints], SeedSequence, BitGenerator, Generator, RandomState}, optional

A seed to initialize the BitGenerator. If None, then fresh, unpredictable entropy will be pulled from the OS. If an int or array_like[ints] is passed, then all values must be non-negative and will be passed to SeedSequence to derive the initial BitGenerator state. One may also pass in a SeedSequence instance. Additionally, when passed a BitGenerator, it will be wrapped by Generator. If passed a Generator, it will be returned unaltered. When passed a legacy RandomState instance it will be coerced to a Generator.

Returns

: Generator

The initialized generator object.

Notes

If seed is not a BitGenerator or a Generator, a new BitGenerator is instantiated. This function does not manage a default global instance.

See seeding_and_entropy for more information about seeding.

Examples

`default_rng` is the recommended constructor for the random number class `Generator`. Here are several ways we can construct a random number generator using `default_rng` and the `Generator` class. Here we use `default_rng` to generate a random float:
import numpy as np
rng = np.random.default_rng(12345)
print(rng)
rfloat = rng.random()
rfloat
type(rfloat)
✓
<class 'float'>
Here we use `default_rng` to generate 3 random integers between 0 (inclusive) and 10 (exclusive):
import numpy as np
rng = np.random.default_rng(12345)
rints = rng.integers(low=0, high=10, size=3)
rints
type(rints[0])
✓
<class 'numpy.int64'>
Here we specify a seed so that we have reproducible results:
import numpy as np
rng = np.random.default_rng(seed=42)
print(rng)
arr1 = rng.random((3, 3))
arr1
✓
array([[0.77395605, 0.43887844, 0.85859792],
       [0.69736803, 0.09417735, 0.97562235],
       [0.7611397 , 0.78606431, 0.12811363]])
If we exit and restart our Python interpreter, we'll see that we generate the same random numbers again:
import numpy as np
rng = np.random.default_rng(seed=42)
arr2 = rng.random((3, 3))
arr2
✓
array([[0.77395605, 0.43887844, 0.85859792],
       [0.69736803, 0.09417735, 0.97562235],
       [0.7611397 , 0.78606431, 0.12811363]])

Aliases

  • numpy.random.default_rng

Referenced by

Other packages