Container for the BitGenerators. ``Generator`` exposes a number of methods for generating random numbers drawn from a variety of probability distributions and serves as a replacement for ``RandomState``. The main difference between the two is that ``Generator`` relies on an add
| 28 | |
| 29 | |
| 30 | class Generator: |
| 31 | """ |
| 32 | Container for the BitGenerators. |
| 33 | |
| 34 | ``Generator`` exposes a number of methods for generating random |
| 35 | numbers drawn from a variety of probability distributions and serves |
| 36 | as a replacement for ``RandomState``. The main difference between the |
| 37 | two is that ``Generator`` relies on an additional ``BitGenerator`` to |
| 38 | manage state and generate the random bits, which are then transformed |
| 39 | into random values from useful distributions. The default ``BitGenerator`` |
| 40 | used by ``Generator`` is ``PCG64``. The ``BitGenerator`` can be changed |
| 41 | by passing an instantiated ``BitGenerator`` to ``Generator``. |
| 42 | |
| 43 | The function :func:`dask.array.random.default_rng` is the recommended way |
| 44 | to instantiate a ``Generator``. |
| 45 | |
| 46 | .. warning:: |
| 47 | |
| 48 | No Compatibility Guarantee. |
| 49 | |
| 50 | ``Generator`` does not provide a version compatibility guarantee. In |
| 51 | particular, as better algorithms evolve the bit stream may change. |
| 52 | |
| 53 | Parameters |
| 54 | ---------- |
| 55 | bit_generator : BitGenerator |
| 56 | BitGenerator to use as the core generator. |
| 57 | |
| 58 | Notes |
| 59 | ----- |
| 60 | In addition to the distribution-specific arguments, each ``Generator`` |
| 61 | method takes a keyword argument `size` that defaults to ``None``. If |
| 62 | `size` is ``None``, then a single value is generated and returned. If |
| 63 | `size` is an integer, then a 1-D array filled with generated values is |
| 64 | returned. If `size` is a tuple, then an array with that shape is |
| 65 | filled and returned. |
| 66 | |
| 67 | The Python stdlib module `random` contains pseudo-random number generator |
| 68 | with a number of methods that are similar to the ones available in |
| 69 | ``Generator``. It uses Mersenne Twister, and this bit generator can |
| 70 | be accessed using ``MT19937``. ``Generator``, besides being |
| 71 | Dask-aware, has the advantage that it provides a much larger number |
| 72 | of probability distributions to choose from. |
| 73 | |
| 74 | All ``Generator`` methods are identical to ``np.random.Generator`` except |
| 75 | that they also take a `chunks=` keyword argument. |
| 76 | |
| 77 | ``Generator`` does not guarantee parity in the generated numbers |
| 78 | with any third party library. In particular, numbers generated by |
| 79 | `Dask` and `NumPy` will differ even if they use the same seed. |
| 80 | |
| 81 | Examples |
| 82 | -------- |
| 83 | >>> from numpy.random import PCG64 |
| 84 | >>> from dask.array.random import Generator |
| 85 | >>> rng = Generator(PCG64()) |
| 86 | >>> rng.standard_normal().compute() # doctest: +SKIP |
| 87 | array(0.44595957) # random |