Debian Codegraph @

docs/source/array-random.rst

raw
1 Random Number Generation
2 ========================
3
4 Dask's random number routines produce pseudo random numbers using combinations
5 of a ``BitGenerator`` to create sequences and a ``Generator`` to use those
6 sequences to sample from different statistical distributions.
7
8 Since Dask version 2023.2.1, the ``Generator`` can be initialized with a number
9 of different ``BitGenerator`` classes. It exposes many different probability
10 distributions. The legacy ``RandomState`` random number routines are still
11 available, but are considered frozen and will not be getting any updates.
12
13 Differences with NumPy
14 ----------------------
15
16 Dask follows the NumPy interface for random number generation with some
17 differences:
18
19 - Methods under ``dask.array.random`` take a ``chunks`` keyword.
20 - Dask tries to be backend agnostic. In other words, you can mostly use CuPy
21 and NumPy interchangeably as a backend for random number generation. Any
22 library providing a similar interface should also work with some effort.
23
24 Notes
25 -----
26
27 - **BitGenerators:** Objects that generate random sequences. These are
28 provided by a backend library such as NumPy or CuPy and are typically
29 unsigned integer words filled with sequences of either 32 or 64 random
30 bits.
31
32 - **Generators:** Objects that transform sequences of random bits from a
33 ``BitGenerator`` into sequences of numbers that follow a specific probability
34 distribution (such as uniform, Normal or Binomial) within a specified
35 interval.
36
37 - Dask does not guarantee that the same number generator is used across versions.
38 This means that numbers generated by ``dask.array.random`` by a new version may
39 not be the same as the previous one, even when the same seed and distribution
40 are used. As better algorithms evolve, the bit stream may change.
41
42 - Dask does not guarantee parity in the generated numbers with any third party
43 library. In particular, numbers generated by Dask and NumPy or CuPy will differ
44 even when given the same seed and ``BitGenerator``. Dask tends to spawn ``SeedSequence``
45 children to produce independent random number streams in parallel.
46
47 - Many of the RandomState methods are exported as functions in ``dask.array.random``.
48 This usage is discouraged, as it is implemented via a global RandomState instance
49 which is not advised on two counts:
50
51 1. It uses global state, which means results will change as the code changes.
52 2. It uses a RandomState rather than the more modern Generator.
53
54 For backward compatible legacy reasons, we cannot change this. Use
55 ``dask.array.random.default_rng()`` to get a Generator and use its methods instead.
56
57 - ``Generator.integers`` is now the canonical way to generate integer random numbers
58 from a discrete uniform distribution. The `endpoint` keyword can be used to
59 specify open or closed intervals. This replaces both `randint` and `random_integers`.
60
61 - ``Generator.random`` is now the canonical way to generate floating-point random
62 numbers, which replaces `random_sample`. The ``dask.array.random.random``
63 method still uses ``RandomState`` for backwards compatibility and should be
64 avoided for new code. Please use ``Generator.random`` instead.
65
66 Quick Start
67 -----------
68
69 Call ``default_rng`` to get a new instance of a Generator, then call its methods to
70 obtain samples from different distributions. By default, ``Generator`` uses bits
71 provided by PCG64 which has better statistical properties than the legacy MT19937
72 used in ``RandomState``.
73
74 .. code-block:: python
75
76 # Do this (new version)
77 import dask.array as da
78 rng = da.random.default_rng()
79 vals = rng.standard_normal(10)
80 more_vals = rng.standard_normal(10)
81
82 # instead of this (legacy version)
83 import dask.array as da
84 vals = da.random.standard_normal(10)
85 more_vals = da.random.standard_normal(10)
86
87
88
89 For further info, please see `NumPy docs <https://numpy.org/devdocs/reference/random/index.html>`_