diff options
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | benchmarks/benchmarks/bench_random.py | 7 | ||||
| -rw-r--r-- | doc/source/reference/random/brng/dsfmt.rst (renamed from doc/source/reference/randomgen/brng/dsfmt.rst) | 4 | ||||
| -rw-r--r-- | doc/source/reference/random/brng/index.rst (renamed from doc/source/reference/randomgen/brng/index.rst) | 4 | ||||
| -rw-r--r-- | doc/source/reference/random/brng/mt19937.rst (renamed from doc/source/reference/randomgen/brng/mt19937.rst) | 4 | ||||
| -rw-r--r-- | doc/source/reference/random/brng/pcg32.rst (renamed from doc/source/reference/randomgen/brng/pcg32.rst) | 4 | ||||
| -rw-r--r-- | doc/source/reference/random/brng/pcg64.rst (renamed from doc/source/reference/randomgen/brng/pcg64.rst) | 4 | ||||
| -rw-r--r-- | doc/source/reference/random/brng/philox.rst (renamed from doc/source/reference/randomgen/brng/philox.rst) | 4 | ||||
| -rw-r--r-- | doc/source/reference/random/brng/threefry.rst (renamed from doc/source/reference/randomgen/brng/threefry.rst) | 4 | ||||
| -rw-r--r-- | doc/source/reference/random/brng/threefry32.rst (renamed from doc/source/reference/randomgen/brng/threefry32.rst) | 4 | ||||
| -rw-r--r-- | doc/source/reference/random/brng/xoroshiro128.rst (renamed from doc/source/reference/randomgen/brng/xoroshiro128.rst) | 4 | ||||
| -rw-r--r-- | doc/source/reference/random/brng/xorshift1024.rst (renamed from doc/source/reference/randomgen/brng/xorshift1024.rst) | 4 | ||||
| -rw-r--r-- | doc/source/reference/random/brng/xoshiro256starstar.rst (renamed from doc/source/reference/randomgen/brng/xoshiro256starstar.rst) | 4 | ||||
| -rw-r--r-- | doc/source/reference/random/brng/xoshiro512starstar.rst (renamed from doc/source/reference/randomgen/brng/xoshiro512starstar.rst) | 4 | ||||
| -rw-r--r-- | doc/source/reference/random/change-log.rst (renamed from doc/source/reference/randomgen/change-log.rst) | 12 | ||||
| -rw-r--r-- | doc/source/reference/random/entropy.rst (renamed from doc/source/reference/randomgen/entropy.rst) | 2 | ||||
| -rw-r--r-- | doc/source/reference/random/extending.rst (renamed from doc/source/reference/randomgen/extending.rst) | 18 | ||||
| -rw-r--r-- | doc/source/reference/random/generator.rst (renamed from doc/source/reference/randomgen/generator.rst) | 4 | ||||
| -rw-r--r-- | doc/source/reference/random/index.rst (renamed from doc/source/reference/randomgen/index.rst) | 87 | ||||
| -rw-r--r-- | doc/source/reference/random/legacy.rst (renamed from doc/source/reference/randomgen/legacy.rst) | 14 | ||||
| -rw-r--r-- | doc/source/reference/random/multithreading.rst (renamed from doc/source/reference/randomgen/multithreading.rst) | 2 | ||||
| -rw-r--r-- | doc/source/reference/random/new-or-different.rst (renamed from doc/source/reference/randomgen/new-or-different.rst) | 55 | ||||
| -rw-r--r-- | doc/source/reference/random/parallel.rst (renamed from doc/source/reference/randomgen/parallel.rst) | 26 | ||||
| -rw-r--r-- | doc/source/reference/random/performance.py (renamed from doc/source/reference/randomgen/performance.py) | 0 | ||||
| -rw-r--r-- | doc/source/reference/random/performance.rst (renamed from doc/source/reference/randomgen/performance.rst) | 16 | ||||
| -rw-r--r-- | doc/source/reference/random/references.rst (renamed from doc/source/reference/randomgen/references.rst) | 0 | ||||
| -rw-r--r-- | doc/source/reference/routines.rst | 2 | ||||
| -rw-r--r-- | numpy/random/_mtrand/_mtrand.pyx (renamed from numpy/random/_mtrand/mtrand.pyx) | 4 | ||||
| -rw-r--r-- | numpy/random/dsfmt.pyx | 20 | ||||
| -rw-r--r-- | numpy/random/generator.pyx | 51 | ||||
| -rw-r--r-- | numpy/random/mt19937.pyx | 18 | ||||
| -rw-r--r-- | numpy/random/mtrand.pyx | 44 | ||||
| -rw-r--r-- | numpy/random/pcg32.pyx | 16 | ||||
| -rw-r--r-- | numpy/random/pcg64.pyx | 16 | ||||
| -rw-r--r-- | numpy/random/philox.pyx | 24 | ||||
| -rw-r--r-- | numpy/random/setup.py | 2 | ||||
| -rw-r--r-- | numpy/random/threefry.pyx | 22 | ||||
| -rw-r--r-- | numpy/random/threefry32.pyx | 22 | ||||
| -rw-r--r-- | numpy/random/xoroshiro128.pyx | 18 | ||||
| -rw-r--r-- | numpy/random/xorshift1024.pyx | 20 | ||||
| -rw-r--r-- | numpy/random/xoshiro256starstar.pyx | 18 | ||||
| -rw-r--r-- | numpy/random/xoshiro512starstar.pyx | 18 |
42 files changed, 326 insertions, 282 deletions
diff --git a/.gitignore b/.gitignore index 485f37957..ec0430dfe 100644 --- a/.gitignore +++ b/.gitignore @@ -175,7 +175,7 @@ benchmarks/env benchmarks/numpy # cythonized files cythonize.dat -numpy/random/mtrand/mtrand.c +numpy/random/_mtrand/_mtrand.c numpy/random/*.c numpy/random/legacy/*.c numpy/random/_mtrand/randint_helpers.pxi diff --git a/benchmarks/benchmarks/bench_random.py b/benchmarks/benchmarks/bench_random.py index 38a079e84..3589c65a3 100644 --- a/benchmarks/benchmarks/bench_random.py +++ b/benchmarks/benchmarks/bench_random.py @@ -4,8 +4,7 @@ from .common import Benchmark import numpy as np -from numpy.random import RandomState -from numpy.random.randomgen import RandomGenerator +from numpy.random import RandomState, RandomGenerator class Random(Benchmark): params = ['normal', 'uniform', 'weibull 1', 'binomial 10 0.5', @@ -95,7 +94,7 @@ class RNG(Benchmark): if brng == 'numpy': self.rg = np.random.RandomState() else: - self.rg = RandomGenerator(getattr(np.random.randomgen, brng)()) + self.rg = RandomGenerator(getattr(np.random, brng)()) self.rg.random_sample() self.int32info = np.iinfo(np.int32) self.uint32info = np.iinfo(np.uint32) @@ -149,7 +148,7 @@ class Bounded(Benchmark): if brng == 'numpy': self.rg = np.random.RandomState() else: - self.rg = RandomGenerator(getattr(np.random.randomgen, brng)()) + self.rg = RandomGenerator(getattr(np.random, brng)()) self.rg.random_sample() def time_bounded(self, brng, args): diff --git a/doc/source/reference/randomgen/brng/dsfmt.rst b/doc/source/reference/random/brng/dsfmt.rst index f9de48d61..a47586a50 100644 --- a/doc/source/reference/randomgen/brng/dsfmt.rst +++ b/doc/source/reference/random/brng/dsfmt.rst @@ -1,9 +1,9 @@ Double SIMD Mersenne Twister (dSFMT) ------------------------------------ -.. module:: numpy.random.randomgen.dsfmt +.. module:: numpy.random.dsfmt -.. currentmodule:: numpy.random.randomgen.dsfmt +.. currentmodule:: numpy.random.dsfmt .. autoclass:: DSFMT diff --git a/doc/source/reference/randomgen/brng/index.rst b/doc/source/reference/random/brng/index.rst index aceecc792..5241f9856 100644 --- a/doc/source/reference/randomgen/brng/index.rst +++ b/doc/source/reference/random/brng/index.rst @@ -1,7 +1,9 @@ Basic Random Number Generators ------------------------------ -The random values produced by :class:`~randomgen.generator.RandomGenerator` +.. currentmodule:: numpy.random + +The random values produced by :class:`~RandomGenerator` are produced by a basic RNG. These basic RNGs do not directly provide random numbers and only contains methods used for seeding, getting or setting the state, jumping or advancing the state, and for accessing diff --git a/doc/source/reference/randomgen/brng/mt19937.rst b/doc/source/reference/random/brng/mt19937.rst index 7739e16ce..1bd3597c8 100644 --- a/doc/source/reference/randomgen/brng/mt19937.rst +++ b/doc/source/reference/random/brng/mt19937.rst @@ -1,9 +1,9 @@ Mersenne Twister (MT19937) -------------------------- -.. module:: numpy.random.randomgen.mt19937 +.. module:: numpy.random.mt19937 -.. currentmodule:: numpy.random.randomgen.mt19937 +.. currentmodule:: numpy.random.mt19937 .. autoclass:: MT19937 :exclude-members: diff --git a/doc/source/reference/randomgen/brng/pcg32.rst b/doc/source/reference/random/brng/pcg32.rst index aaf3929e8..f079f5a8c 100644 --- a/doc/source/reference/randomgen/brng/pcg32.rst +++ b/doc/source/reference/random/brng/pcg32.rst @@ -1,9 +1,9 @@ Parallel Congruent Generator (32-bit, PCG32) -------------------------------------------- -.. module:: numpy.random.randomgen.pcg32 +.. module:: numpy.random.pcg32 -.. currentmodule:: numpy.random.randomgen.pcg32 +.. currentmodule:: numpy.random.pcg32 .. autoclass:: PCG32 :exclude-members: diff --git a/doc/source/reference/randomgen/brng/pcg64.rst b/doc/source/reference/random/brng/pcg64.rst index 94e73e491..93f026fcb 100644 --- a/doc/source/reference/randomgen/brng/pcg64.rst +++ b/doc/source/reference/random/brng/pcg64.rst @@ -1,9 +1,9 @@ Parallel Congruent Generator (64-bit, PCG64) -------------------------------------------- -.. module:: numpy.random.randomgen.pcg64 +.. module:: numpy.random.pcg64 -.. currentmodule:: numpy.random.randomgen.pcg64 +.. currentmodule:: numpy.random.pcg64 .. autoclass:: PCG64 :exclude-members: diff --git a/doc/source/reference/randomgen/brng/philox.rst b/doc/source/reference/random/brng/philox.rst index 091c4d3e0..c1e047c54 100644 --- a/doc/source/reference/randomgen/brng/philox.rst +++ b/doc/source/reference/random/brng/philox.rst @@ -1,9 +1,9 @@ Philox Counter-based RNG ------------------------ -.. module:: numpy.random.randomgen.philox +.. module:: numpy.random.philox -.. currentmodule:: numpy.random.randomgen.philox +.. currentmodule:: numpy.random.philox .. autoclass:: Philox :exclude-members: diff --git a/doc/source/reference/randomgen/brng/threefry.rst b/doc/source/reference/random/brng/threefry.rst index 4f5c56bae..eefe16ea0 100644 --- a/doc/source/reference/randomgen/brng/threefry.rst +++ b/doc/source/reference/random/brng/threefry.rst @@ -1,9 +1,9 @@ ThreeFry Counter-based RNG -------------------------- -.. module:: numpy.random.randomgen.threefry +.. module:: numpy.random.threefry -.. currentmodule:: numpy.random.randomgen.threefry +.. currentmodule:: numpy.random.threefry .. autoclass:: ThreeFry :exclude-members: diff --git a/doc/source/reference/randomgen/brng/threefry32.rst b/doc/source/reference/random/brng/threefry32.rst index bd85db4a7..f0d3dc281 100644 --- a/doc/source/reference/randomgen/brng/threefry32.rst +++ b/doc/source/reference/random/brng/threefry32.rst @@ -1,9 +1,9 @@ ThreeFry32 Counter-based RNG ---------------------------- -.. module:: numpy.random.randomgen.threefry32 +.. module:: numpy.random.threefry32 -.. currentmodule:: numpy.random.randomgen.threefry32 +.. currentmodule:: numpy.random.threefry32 .. autoclass:: ThreeFry32 :exclude-members: diff --git a/doc/source/reference/randomgen/brng/xoroshiro128.rst b/doc/source/reference/random/brng/xoroshiro128.rst index 6796c4457..590552236 100644 --- a/doc/source/reference/randomgen/brng/xoroshiro128.rst +++ b/doc/source/reference/random/brng/xoroshiro128.rst @@ -1,9 +1,9 @@ Xoroshiro128+ ------------- -.. module:: numpy.random.randomgen.xoroshiro128 +.. module:: numpy.random.xoroshiro128 -.. currentmodule:: numpy.random.randomgen.xoroshiro128 +.. currentmodule:: numpy.random.xoroshiro128 .. autoclass:: Xoroshiro128 :exclude-members: diff --git a/doc/source/reference/randomgen/brng/xorshift1024.rst b/doc/source/reference/random/brng/xorshift1024.rst index 64df7e050..24ed3df04 100644 --- a/doc/source/reference/randomgen/brng/xorshift1024.rst +++ b/doc/source/reference/random/brng/xorshift1024.rst @@ -1,9 +1,9 @@ Xorshift1024*φ -------------- -.. module:: numpy.random.randomgen.xorshift1024 +.. module:: numpy.random.xorshift1024 -.. currentmodule:: numpy.random.randomgen.xorshift1024 +.. currentmodule:: numpy.random.xorshift1024 .. autoclass:: Xorshift1024 :exclude-members: diff --git a/doc/source/reference/randomgen/brng/xoshiro256starstar.rst b/doc/source/reference/random/brng/xoshiro256starstar.rst index 7603e6f1b..85c445666 100644 --- a/doc/source/reference/randomgen/brng/xoshiro256starstar.rst +++ b/doc/source/reference/random/brng/xoshiro256starstar.rst @@ -1,9 +1,9 @@ Xoshiro256** ------------ -.. module:: numpy.random.randomgen.xoshiro256starstar +.. module:: numpy.random.xoshiro256starstar -.. currentmodule:: numpy.random.randomgen.xoshiro256starstar +.. currentmodule:: numpy.random.xoshiro256starstar .. autoclass:: Xoshiro256StarStar :exclude-members: diff --git a/doc/source/reference/randomgen/brng/xoshiro512starstar.rst b/doc/source/reference/random/brng/xoshiro512starstar.rst index 64f95f750..0c008d56e 100644 --- a/doc/source/reference/randomgen/brng/xoshiro512starstar.rst +++ b/doc/source/reference/random/brng/xoshiro512starstar.rst @@ -1,9 +1,9 @@ Xoshiro512** ------------ -.. module:: numpy.random.randomgen.xoshiro512starstar +.. module:: numpy.random.xoshiro512starstar -.. currentmodule:: numpy.random.randomgen.xoshiro512starstar +.. currentmodule:: numpy.random.xoshiro512starstar .. autoclass:: Xoshiro512StarStar :exclude-members: diff --git a/doc/source/reference/randomgen/change-log.rst b/doc/source/reference/random/change-log.rst index f791c8f54..af3c266ef 100644 --- a/doc/source/reference/randomgen/change-log.rst +++ b/doc/source/reference/random/change-log.rst @@ -1,5 +1,5 @@ -Change Log ----------- +Change Log for the original bashtage/randomgen repo +--------------------------------------------------- v1.16.1 ======= - Synchronized with upstream changes. @@ -7,12 +7,12 @@ v1.16.1 v1.16.0 ======= -- Fixed a bug that affected :class:`~randomgen.dsfmt.DSFMT` when calling - :func:`~randomgen.dsfmt.DSFMT.jump` or :func:`~randomgen.dsfmt.DSFMT.seed` +- Fixed a bug that affected ``randomgen.dsfmt.DSFMT`` when calling + ``~randomgen.dsfmt.DSFMT.jump`` or ``randomgen.dsfmt.DSFMT.seed`` that failed to reset the buffer. This resulted in upto 381 values from the previous state being used before the buffer was refilled at the new state. -- Fixed bugs in :class:`~randomgen.xoshiro512starstar.Xoshiro512StarStar` - and :class:`~randomgen.xorshift1024.Xorshift1024` where the fallback +- Fixed bugs in ``randomgen.xoshiro512starstar.Xoshiro512StarStar`` + and ``randomgen.xorshift1024.Xorshift1024`` where the fallback entropy initialization used too few bytes. This bug is unlikely to be encountered since this path is only encountered if the system random number generator fails. diff --git a/doc/source/reference/randomgen/entropy.rst b/doc/source/reference/random/entropy.rst index 6814edfbe..0664da6f9 100644 --- a/doc/source/reference/randomgen/entropy.rst +++ b/doc/source/reference/random/entropy.rst @@ -1,6 +1,6 @@ System Entropy ============== -.. module:: numpy.random.randomgen.entropy +.. module:: numpy.random.entropy .. autofunction:: random_entropy diff --git a/doc/source/reference/randomgen/extending.rst b/doc/source/reference/random/extending.rst index 64f2ddcc1..f76e3984f 100644 --- a/doc/source/reference/randomgen/extending.rst +++ b/doc/source/reference/random/extending.rst @@ -1,8 +1,10 @@ +.. currentmodule:: numpy.random + Extending --------- The basic RNGs have been designed to be extendable using standard tools for high-performance Python -- numba and Cython. -The :class:`randomgen.generator.RandomGenerator` object can also be used with +The `~RandomGenerator` object can also be used with user-provided basic RNGs as long as these export a small set of required functions. @@ -17,7 +19,7 @@ provided by ``ctypes.next_double``. .. code-block:: python - from randomgen import Xoroshiro128 + from numpy.random import Xoroshiro128 import numpy as np import numba as nb @@ -65,7 +67,7 @@ Cython ====== Cython can be used to unpack the ``PyCapsule`` provided by a basic RNG. -This example uses :class:`~randomgen.xoroshiro128.Xoroshiro128` and +This example uses `~xoroshiro128.Xoroshiro128` and ``random_gauss_zig``, the Ziggurat-based generator for normals, to fill an array. The usual caveats for writing high-performance code using Cython -- removing bounds checks and wrap around, providing array alignment information @@ -77,9 +79,9 @@ removing bounds checks and wrap around, providing array alignment information cimport numpy as np cimport cython from cpython.pycapsule cimport PyCapsule_IsValid, PyCapsule_GetPointer - from randomgen.common cimport * - from randomgen.distributions cimport random_gauss_zig - from randomgen.xoroshiro128 import Xoroshiro128 + from numpy.random.common cimport * + from numpy.random.distributions cimport random_gauss_zig + from numpy.random.xoroshiro128 import Xoroshiro128 @cython.boundscheck(False) @@ -137,7 +139,7 @@ examples folder. New Basic RNGs ============== -:class:`~randomgen.generator.RandomGenerator` can be used with other +`~RandomGenerator` can be used with other user-provided basic RNGs. The simplest way to write a new basic RNG is to examine the pyx file of one of the existing basic RNGs. The key structure that must be provided is the ``capsule`` which contains a ``PyCapsule`` to a @@ -158,7 +160,7 @@ used by the basic RNG. The next three are function pointers which return the next 64- and 32-bit unsigned integers, the next random double and the next raw value. This final function is used for testing and so can be set to the next 64-bit unsigned integer function if not needed. Functions inside -:class:`~randomgen.generator.RandomGenerator` use this structure as in +``RandomGenerator`` use this structure as in .. code-block:: c diff --git a/doc/source/reference/randomgen/generator.rst b/doc/source/reference/random/generator.rst index 54325f4d3..9d248732f 100644 --- a/doc/source/reference/randomgen/generator.rst +++ b/doc/source/reference/random/generator.rst @@ -1,8 +1,8 @@ -.. currentmodule:: numpy.random.randomgen +.. currentmodule:: numpy.random Random Generator ---------------- -The :class:`~RandomGenerator` provides access to +The `~RandomGenerator` provides access to a wide range of distributions, and served as a replacement for :class:`~numpy.random.RandomState`. The main difference between the two is that ``RandomGenerator`` relies on an additional basic RNG to diff --git a/doc/source/reference/randomgen/index.rst b/doc/source/reference/random/index.rst index a8b6f1c9b..1cbf5c685 100644 --- a/doc/source/reference/randomgen/index.rst +++ b/doc/source/reference/random/index.rst @@ -1,49 +1,46 @@ -.. current_module numpy.random.randomgen +.. currentmodule:: numpy.random -numpy.random.randomgen -====================== +numpy.random +============ -This package modernizes the legacy -`~numpy.random.RandomState` object and allows changing the core random -number generator (RNG). The `~numpy.random.randomgen.RandomGenerator` can -be initialized with a number of different RNGs, and exposes many different -probability distributions. +A `~RandomGenerator` can +be initialized with a number of different Random Number Generators (RNG)s, and +exposes many different probability distributions. Quick Start ----------- -By default, `generator.RandomGenerator` uses normals provided by +By default, `RandomGenerator` uses normals provided by `xoroshiro128.Xoroshiro128` which will be faster than the legacy methods in -`numpy.random` +`numpy.random.RandomState` .. code-block:: python - # As replacement for numpy.random - import randomgen.generator as random + # As replacement for numpy.random.RandomState + from numpy import random random.standard_normal() -`numpy.random.randomgen.RandomGenerator` can also be used as a replacement for -`~numpy.random.RandomState`, although the random values are generated by -`~numpyrandom.randomgen.xoroshiro128.Xoroshiro128`. Since ``randomgen`` -separates the `~numpy.random.randomgen.RandomGenerator` from the RNG, it is not -possible to directly seed the generator. +`RandomGenerator` can be used as a direct replacement for +`~RandomState`, although the random values are generated by +`~xoroshiro128.Xoroshiro128`. The `RandomGenerator` holds an instance of a RNG. +It is accessable as ``gen.brng``. .. code-block:: python # As replacement for RandomState() - from randomgen import RandomGenerator + from numpy.random import RandomGenerator rg = RandomGenerator() rg.standard_normal() -Seeds can be passed to any of the basic RNGs. Here `numpy.random.randomgen. -mt19937.MT19937` is used and the `~numpy.random.randomgen.RandomGenerator` is -accessed via the attribute `~numpy.random.randomgen.mt19937.MT19937.generator`. +Seeds can be passed to any of the basic RNGs. Here `mt19937.MT19937` is used +and the ``RandomGenerator`` is accessed via the attribute `mt19937.MT19937. +generator`. .. code-block:: python - from randomgen import MT19937 + from numpy.random import MT19937 rg = MT19937(12345).generator rg.standard_normal() @@ -59,20 +56,20 @@ underlying RNG state and provides functions to produce random doubles and random unsigned 32- and 64-bit values. The basic random generator also handles all seeding since this varies when using alternative basic RNGs. -The `random generator <~numpy.random.randomgen.RandomGenerator>` takes the +The `random generator <~RandomGenerator>` takes the basic RNG-provided functions and transforms them into more useful distributions, e.g., simulated normal random values. This structure allows alternative basic RNGs to be used without code duplication. -The `~numpy.random.randomgen.RandomGenerator` is the user-facing object -that is nearly identical to :class:`~numpy.random.RandomState`. The canonical -method to initialize a generator passes a basic RNG -- `~numpy.random. -randomgen.mt19937.MT19937`, the underlying RNG in NumPy -- as the -sole argument. Note that the basic RNG must be instantized. +The ``RandomGenerator`` is the user-facing object +that is nearly identical to :class:`~.RandomState`. The canonical +method to initialize a generator passes a basic RNG -- `~mt19937.MT19937`, the +underlying RNG in NumPy -- as the sole argument. Note that the basic RNG must +be instantized. .. code-block:: python - from randomgen import RandomGenerator, MT19937 + from numpy.random import RandomGenerator, MT19937 rg = RandomGenerator(MT19937()) rg.random_sample() @@ -83,9 +80,8 @@ Seed information is directly passed to the basic RNG. rg = RandomGenerator(MT19937(12345)) rg.random_sample() -A shorthand method is also available which uses the `~numpy.random.randomgen. -mt19937.MT19937.generator` property from a basic RNG to access an embedded -random generator. +A shorthand method is also available which uses the `~mt19937.MT19937. +generator` property from a basic RNG to access an embedded random generator. .. code-block:: python @@ -97,11 +93,10 @@ What's New or Different .. warning:: The Box-Muller method used to produce NumPy's normals is no longer available - in `~numpy.random.randomgen.RandomGenerator`. It is not possible to - reproduce the random values using `~numpy.random.randomgen.RandomGenerator` - for the normal distribution or any other distribution that - relies on the normal such as the gamma or student's t. If you require - backward compatibility, a legacy generator, `~numpy.random.randomgen.legacy. + in `~RandomGenerator`. It is not possible to reproduce the random values + using ``RandomGenerator`` for the normal distribution or any other + distribution that relies on the normal such as the gamma or student's t. + If you require backward compatibility, a legacy generator, `~legacy. LegacyGenerator`, has been created which can fully reproduce the sequence produced by NumPy. @@ -113,18 +108,18 @@ What's New or Different select distributions * Optional ``out`` argument that allows existing arrays to be filled for select distributions -* `~numpy.random.randomgen.entropy.random_entropy` provides access to the system +* `~entropy.random_entropy` provides access to the system source of randomness that is used in cryptographic applications (e.g., ``/dev/urandom`` on Unix). * All basic random generators functions can produce doubles, uint64s and - uint32s via CTypes (`~numpy.random.randomgen.xoroshiro128.Xoroshiro128.ctypes`) - and CFFI (:meth:`~numpy.random.randomgen.xoroshiro128.Xoroshiro128.cffi`). + uint32s via CTypes (`~xoroshiro128.Xoroshiro128.ctypes`) + and CFFI (:meth:`~xoroshiro128.Xoroshiro128.cffi`). This allows these basic RNGs to be used in numba. * The basic random number generators can be used in downstream projects via :ref:`Cython <randomgen_cython>`. * Support for Lemire’s method [Lemire]_ of generating uniform integers on an arbitrary interval by setting ``use_masked=True`` in - `~umpy.random.randomgen.generator.RandomGenerator.randint`. + `~RandomGenerator.randint`. See :ref:`new-or-different` for a complete list of improvements and @@ -146,7 +141,7 @@ generators, 'in addition' to the standard PRNG in NumPy. The included PRNGs are * MT19937 - The standard NumPy generator. Produces identical results to NumPy using the same seed/state. Adds a - `~numpy.random.randomgen.mt19937.MT19937.jump` function that advances the + `~mt19937.MT19937.jump` function that advances the generator as-if ``2**128`` draws have been made. See `numpy.random`. * dSFMT - SSE2 enabled versions of the MT19937 generator. Theoretically the same, but with a different state and so it is not possible to produce a @@ -155,24 +150,24 @@ generators, 'in addition' to the standard PRNG in NumPy. The included PRNGs are * XoroShiro128+ - Improved version of XorShift128+ with better performance and statistical quality. Like the XorShift generators, it can be jumped to produce multiple streams in parallel applications. See - `~numpy.random.randomgen.xoroshiro128.Xoroshiro128.jump` for details. + `~xoroshiro128.Xoroshiro128.jump` for details. More information about this PRNG is available at the `xorshift, xoroshiro and xoshiro authors' page`_. * XorShift1024*φ - Fast fast generator based on the XSadd generator. Supports ``jump`` and so can be used in parallel applications. See the documentation for - `~numpy.random.randomgen.xorshift1024.Xorshift1024.jump` for details. More + `~xorshift1024.Xorshift1024.jump` for details. More information about these PRNGs is available at the `xorshift, xoroshiro and xoshiro authors' page`_. * Xorshiro256** and Xorshiro512** - The most recently introduced XOR, shift, and rotate generator. Supports ``jump`` and so can be used in parallel applications. See the documentation for - `~numpy.random.randomgen.xoshiro256starstar.Xoshirt256StarStar.jump` for + `~xoshiro256starstar.Xoshirt256StarStar.jump` for details. More information about these PRNGs is available at the `xorshift, xoroshiro and xoshiro authors' page`_. * PCG-64 - Fast generator that support many parallel streams and can be advanced by an arbitrary amount. See the documentation for - `~numpy.random.randomgen.pcg64.PCG64.advance`. PCG-64 has a period of + `~pcg64.PCG64.advance`. PCG-64 has a period of :math:`2^{128}`. See the `PCG author's page`_ for more details about this class of PRNG. * ThreeFry and Philox - counter-based generators capable of being advanced an diff --git a/doc/source/reference/randomgen/legacy.rst b/doc/source/reference/random/legacy.rst index 7e87f871c..b9bfabcc7 100644 --- a/doc/source/reference/randomgen/legacy.rst +++ b/doc/source/reference/random/legacy.rst @@ -1,15 +1,15 @@ Legacy Random Generation ------------------------ -The :class:`~randomgen.legacy.LegacyGenerator` provides access to -some legacy generators. These all depend on Box-Muller normals or +The :class:`~legacy.LegacyGenerator` provides access to +legacy generators. These all depend on Box-Muller normals or inverse CDF exponentials or gammas. This class should only be used if it is essential to have randoms that are identical to what would have been produced by NumPy. -:class:`~randomgen.legacy.LegacyGenerator` add additional information +:class:`~legacy.LegacyGenerator` add additional information to the state which is required when using Box-Muller normals since these are produced in pairs. It is important to use -:attr:`~randomgen.legacy.LegacyGenerator.state` +:attr:`~legacy.LegacyGenerator.state` when accessing the state so that these extra values are saved. .. warning:: @@ -27,8 +27,8 @@ when accessing the state so that these extra values are saved. .. code-block:: python - from randomgen import RandomGenerator, MT19937 - from randomgen.legacy import LegacyGenerator + from numpy.random import MT19937 + from numpy.random.legacy import LegacyGenerator from numpy.random import RandomState # Use same seed rs = RandomState(12345) @@ -47,7 +47,7 @@ when accessing the state so that these extra values are saved. lg.standard_exponential() -.. currentmodule:: numpy.random.randomgen.legacy +.. currentmodule:: numpy.random.legacy .. autoclass:: LegacyGenerator :exclude-members: diff --git a/doc/source/reference/randomgen/multithreading.rst b/doc/source/reference/random/multithreading.rst index 6efbcdbe7..f7762b73a 100644 --- a/doc/source/reference/randomgen/multithreading.rst +++ b/doc/source/reference/random/multithreading.rst @@ -16,7 +16,7 @@ that the same seed will produce the same outputs. .. code-block:: ipython - from randomgen import Xorshift1024 + from numpy.random import Xorshift1024 import multiprocessing import concurrent.futures import numpy as np diff --git a/doc/source/reference/randomgen/new-or-different.rst b/doc/source/reference/random/new-or-different.rst index 089efd6fb..7ddebb5b2 100644 --- a/doc/source/reference/randomgen/new-or-different.rst +++ b/doc/source/reference/random/new-or-different.rst @@ -1,41 +1,44 @@ .. _new-or-different: +.. currentmodule:: numpy.random + What's New or Different ----------------------- .. warning:: The Box-Muller method used to produce NumPy's normals is no longer available - in :class:`~randomgen.generator.RandomGenerator`. It is not possible to - reproduce the random values using :class:`~randomgen.generator.RandomGenerator` - for the normal distribution or any other distribution that relies on the - normal such as the gamma or student's t. If you require backward compatibility, a - legacy generator, :class:`~randomgen.legacy.LegacyGenerator`, has been created - which can fully reproduce the sequence produced by NumPy. + in `~.RandomGenerator`. It is not possible to + reproduce the exact random values using ``RandomGenerator`` for the normal + distribution or any other distribution that relies on the normal such as the + `numpy.random.gamma` or `numpy.random.standard_t`. If you require backward + compatibility, a legacy generator, `~.legacy. + LegacyGenerator`, has been created which can fully reproduce the exact byte + sequence produced by legacy code. -* :func:`~randomgen.entropy.random_entropy` provides access to the system +* `~.entropy.random_entropy` provides access to the system source of randomness that is used in cryptographic applications (e.g., ``/dev/urandom`` on Unix). * Simulate from the complex normal distribution - (:meth:`~randomgen.generator.RandomGenerator.complex_normal`) + (`~.RandomGenerator.complex_normal`) * The normal, exponential and gamma generators use 256-step Ziggurat methods which are 2-10 times faster than NumPy's default implementation in - :meth:`~randomgen.generator.RandomGenerator.standard_normal`, - :meth:`~randomgen.generator.RandomGenerator.standard_exponential` or - :meth:`~randomgen.generator.RandomGenerator.standard_gamma`. + `~.RandomGenerator.standard_normal`, + `~.RandomGenerator.standard_exponential` or + `~.RandomGenerator.standard_gamma`. * The Box-Muller used to produce NumPy's normals is no longer available. * All basic random generators functions to produce doubles, uint64s and - uint32s via CTypes (:meth:`~randomgen.xoroshiro128.Xoroshiro128.ctypes`) - and CFFI (:meth:`~randomgen.xoroshiro128.Xoroshiro128.cffi`). This allows - these basic RNGs to be used in numba. + uint32s via CTypes (`~.xoroshiro128.Xoroshiro128. + ctypes`) and CFFI (`~.xoroshiro128.Xoroshiro128.cffi`). + This allows these basic RNGs to be used in numba. * The basic random number generators can be used in downstream projects via Cython. .. ipython:: python - from numpy.random.randomgen import Xoroshiro128 + from numpy.random import Xoroshiro128 import numpy.random rg = Xoroshiro128().generator %timeit rg.standard_normal(100000) @@ -55,12 +58,12 @@ What's New or Different to produce either single or double prevision uniform random variables for select distributions - * Uniforms (:meth:`~randomgen.generator.RandomGenerator.random_sample` and - :meth:`~randomgen.generator.RandomGenerator.rand`) - * Normals (:meth:`~randomgen.generator.RandomGenerator.standard_normal` and - :meth:`~randomgen.generator.RandomGenerator.randn`) - * Standard Gammas (:meth:`~randomgen.generator.RandomGenerator.standard_gamma`) - * Standard Exponentials (:meth:`~randomgen.generator.RandomGenerator.standard_exponential`) + * Uniforms (`~.RandomGenerator.random_sample` and + `~.RandomGenerator.rand`) + * Normals (`~.RandomGenerator.standard_normal` and + `~.RandomGenerator.randn`) + * Standard Gammas (`~.RandomGenerator.standard_gamma`) + * Standard Exponentials (`~.RandomGenerator.standard_exponential`) .. ipython:: python @@ -72,10 +75,10 @@ What's New or Different * Optional ``out`` argument that allows existing arrays to be filled for select distributions - * Uniforms (:meth:`~randomgen.generator.RandomGenerator.random_sample`) - * Normals (:meth:`~randomgen.generator.RandomGenerator.standard_normal`) - * Standard Gammas (:meth:`~randomgen.generator.RandomGenerator.standard_gamma`) - * Standard Exponentials (:meth:`~randomgen.generator.RandomGenerator.standard_exponential`) + * Uniforms (`~.RandomGenerator.random_sample`) + * Normals (`~.RandomGenerator.standard_normal`) + * Standard Gammas (`~.RandomGenerator.standard_gamma`) + * Standard Exponentials (`~.RandomGenerator.standard_exponential`) This allows multithreading to fill large arrays in chunks using suitable PRNGs in parallel. @@ -90,7 +93,7 @@ What's New or Different * Support for Lemire’s method of generating uniform integers on an arbitrary interval by setting ``use_masked=True`` in - (:meth:`~randomgen.generator.RandomGenerator.randint`). + (`~.RandomGenerator.randint`). .. ipython:: python diff --git a/doc/source/reference/randomgen/parallel.rst b/doc/source/reference/random/parallel.rst index df6f58d75..41e47039d 100644 --- a/doc/source/reference/randomgen/parallel.rst +++ b/doc/source/reference/random/parallel.rst @@ -7,18 +7,20 @@ or distributed). .. _independent-streams: +.. currentmodule:: numpy.random + Independent Streams ------------------- -:class:`~randomgen.pcg64.PCG64`, :class:`~randomgen.threefry.ThreeFry` -and :class:`~randomgen.philox.Philox` support independent streams. This +:class:`~pcg64.PCG64`, :class:`~threefry.ThreeFry` +and :class:`~philox.Philox` support independent streams. This example shows how many streams can be created by passing in different index values in the second input while using the same seed in the first. .. code-block:: python - from randomgen.entropy import random_entropy - from randomgen import PCG64 + from numpy.random.entropy import random_entropy + from numpy.random import PCG64 entropy = random_entropy(4) # 128-bit number as a seed @@ -26,14 +28,14 @@ values in the second input while using the same seed in the first. streams = [PCG64(seed, stream) for stream in range(10)] -:class:`~randomgen.philox.Philox` and :class:`~randomgen.threefry.ThreeFry` are +:class:`~philox.Philox` and :class:`~threefry.ThreeFry` are counter-based RNGs which use a counter and key. Different keys can be used to produce independent streams. .. code-block:: python import numpy as np - from randomgen import ThreeFry + from numpy.random import ThreeFry key = random_entropy(8) key = key.view(np.uint64) @@ -81,8 +83,8 @@ overlap. .. code-block:: python - from randomgen.entropy import random_entropy - from randomgen import Xorshift1024 + from numpy.random.entropy import random_entropy + from numpy.random import Xorshift1024 entropy = random_entropy(2).astype(np.uint64) # 64-bit number as a seed @@ -97,8 +99,8 @@ overlap. Advance ******* ``advance`` can be used to jump the state an arbitrary number of steps, and so -is a more general approach than ``jump``. :class:`~randomgen.pcg64.PCG64`, -:class:`~randomgen.threefry.ThreeFry` and :class:`~randomgen.philox.Philox` +is a more general approach than ``jump``. :class:`~pcg64.PCG64`, +:class:`~threefry.ThreeFry` and :class:`~philox.Philox` support ``advance``, and since these also support independent streams, it is not usually necessary to use ``advance``. @@ -119,12 +121,12 @@ This occurs for two reasons: Advancing the PRNG state resets any pre-computed random numbers. This is required to ensure exact reproducibility. -This example uses ``advance`` to advance a :class:`~randomgen.pcg64.PCG64` +This example uses ``advance`` to advance a :class:`~pcg64.PCG64` generator 2 ** 127 steps to set a sequence of random number generators. .. code-block:: python - from randomgen import PCG64 + from numpy.random import PCG64 brng = PCG64() brng_copy = PCG64() brng_copy.state = brng.state diff --git a/doc/source/reference/randomgen/performance.py b/doc/source/reference/random/performance.py index 12cbbc5d3..12cbbc5d3 100644 --- a/doc/source/reference/randomgen/performance.py +++ b/doc/source/reference/random/performance.py diff --git a/doc/source/reference/randomgen/performance.rst b/doc/source/reference/random/performance.rst index 2dfb32101..321d49454 100644 --- a/doc/source/reference/randomgen/performance.rst +++ b/doc/source/reference/random/performance.rst @@ -1,26 +1,28 @@ Performance ----------- -.. py:module:: randomgen +.. py:module:: numpy.random + +.. currentmodule:: numpy.random Recommendation ************** The recommended generator for single use is -:class:`~randomgen.xoroshiro128.Xoroshiro128`. The recommended generator +:class:`~xoroshiro128.Xoroshiro128`. The recommended generator for use in large-scale parallel applications is -:class:`~randomgen.xorshift1024.Xorshift1024` +:class:`~xorshift1024.Xorshift1024` where the `jump` method is used to advance the state. For very large scale applications -- requiring 1,000+ independent streams, -:class:`~randomgen.pcg64.PCG64` or :class:`~randomgen.threefry.ThreeFry` are +:class:`~pcg64.PCG64` or :class:`~threefry.ThreeFry` are the best choices. Timings ******* The timings below are the time in ms to produce 1,000,000 random values from a -specific distribution. :class:`~randomgen.xoroshiro128.Xoroshiro128` is the -fastest, followed by :class:`~randomgen.xorshift1024.Xorshift1024` and -:class:`~randomgen.pcg64.PCG64`. The original :class:`~randomgen.mt19937.MT19937` +specific distribution. :class:`~xoroshiro128.Xoroshiro128` is the +fastest, followed by :class:`~xorshift1024.Xorshift1024` and +:class:`~pcg64.PCG64`. The original :class:`~mt19937.MT19937` generator is much slower since it requires 2 32-bit values to equal the output of the faster generators. diff --git a/doc/source/reference/randomgen/references.rst b/doc/source/reference/random/references.rst index 0dc99868f..0dc99868f 100644 --- a/doc/source/reference/randomgen/references.rst +++ b/doc/source/reference/random/references.rst diff --git a/doc/source/reference/routines.rst b/doc/source/reference/routines.rst index 0ed99cbda..e3fbc2be6 100644 --- a/doc/source/reference/routines.rst +++ b/doc/source/reference/routines.rst @@ -42,7 +42,7 @@ indentation. routines.padding routines.polynomials routines.random - randomgen/index + random/index routines.set routines.sort routines.statistics diff --git a/numpy/random/_mtrand/mtrand.pyx b/numpy/random/_mtrand/_mtrand.pyx index 329d5cd68..42e40e87e 100644 --- a/numpy/random/_mtrand/mtrand.pyx +++ b/numpy/random/_mtrand/_mtrand.pyx @@ -1918,9 +1918,9 @@ cdef class RandomState: the probability density function: >>> import matplotlib.pyplot as plt - >>> import scipy.special as sps + >>> import scipy.special as sps # doctest: +SKIP >>> count, bins, ignored = plt.hist(s, 50, density=True) - >>> y = bins**(shape-1) * ((np.exp(-bins/scale))/ \\ + >>> y = bins**(shape-1) * ((np.exp(-bins/scale))/ # doctest: +SKIP ... (sps.gamma(shape) * scale**shape)) >>> plt.plot(bins, y, linewidth=2, color='r') >>> plt.show() diff --git a/numpy/random/dsfmt.pyx b/numpy/random/dsfmt.pyx index 9a7199e85..709c2c8e8 100644 --- a/numpy/random/dsfmt.pyx +++ b/numpy/random/dsfmt.pyx @@ -102,13 +102,13 @@ cdef class DSFMT: generators should be initialized with the same seed to ensure that the segments come from the same sequence. - >>> from numpy.random.randomgen.entropy import random_entropy - >>> from numpy.random.randomgen import RandomGenerator, DSFMT + >>> from numpy.random.entropy import random_entropy + >>> from numpy.random import RandomGenerator, DSFMT >>> seed = random_entropy() >>> rs = [RandomGenerator(DSFMT(seed)) for _ in range(10)] - # Advance rs[i] by i jumps + # Advance each DSFMT instance by i jumps >>> for i in range(10): - ... rs[i].jump() + ... rs[i].brng.jump() **State and Seeding** @@ -182,10 +182,12 @@ cdef class DSFMT: self.state) def __dealloc__(self): - PyArray_free_aligned(self.rng_state.state) - PyArray_free_aligned(self.rng_state.buffered_uniforms) - free(self.rng_state) - free(self._brng) + if self.rng_state: + PyArray_free_aligned(self.rng_state.state) + PyArray_free_aligned(self.rng_state.buffered_uniforms) + free(self.rng_state) + if self._brng: + free(self._brng) cdef _reset_state_variables(self): self.rng_state.buffer_loc = DSFMT_N64 @@ -396,7 +398,7 @@ cdef class DSFMT: Returns ------- - gen : numpy.random.randomgen.generator.RandomGenerator + gen : numpy.random.RandomGenerator # ignore Random generator used this instance as the basic RNG """ if self._generator is None: diff --git a/numpy/random/generator.pyx b/numpy/random/generator.pyx index e68edb98a..7f18392a8 100644 --- a/numpy/random/generator.pyx +++ b/numpy/random/generator.pyx @@ -58,19 +58,22 @@ cdef class RandomGenerator: Examples -------- - >>> from np.random.randomgen import RandomGenerator + >>> from numpy.random import RandomGenerator >>> rg = RandomGenerator() >>> rg.standard_normal() + -0.203 # random Using a specific generator - >>> from np.random.randomgen import MT19937 + >>> from numpy.random import MT19937 >>> rg = RandomGenerator(MT19937()) The generator is also directly available from basic RNGs >>> rg = MT19937().generator >>> rg.standard_normal() + -0.203 # random + """ cdef public object brng cdef brng_t *_brng @@ -92,7 +95,8 @@ cdef class RandomGenerator: self.lock = brng.lock def __dealloc__(self): - free(self._binomial) + if self._binomial: + free(self._binomial) def __repr__(self): return self.__str__() + ' at 0x{:X}'.format(id(self)) @@ -351,14 +355,12 @@ cdef class RandomGenerator: Examples -------- - >>> rg = np.random.randomgen.RandomGenerator() # need a RandomGenerator object + >>> rg = np.random.RandomGenerator() # need a RandomGenerator object >>> rg.tomaxint((2,2,2)) array([[[1170048599, 1600360186], # random [ 739731006, 1947757578]], [[1871712945, 752307660], [1601631370, 1479324245]]]) - >>> np.iinfo(np.int).max - 2147483647 >>> rg.tomaxint((2,2,2)) < np.iinfo(np.int).max array([[[ True, True], [ True, True]], @@ -1275,11 +1277,11 @@ cdef class RandomGenerator: the probability density function: >>> import matplotlib.pyplot as plt - >>> import scipy.special as sps + >>> import scipy.special as sps # doctest: +SKIP >>> count, bins, ignored = plt.hist(s, 50, density=True) - >>> y = bins**(shape-1) * ((np.exp(-bins/scale))/ \\ + >>> y = bins**(shape-1) * ((np.exp(-bins/scale))/ # doctest: +SKIP ... (sps.gamma(shape) * scale**shape)) - >>> plt.plot(bins, y, linewidth=2, color='r') + >>> plt.plot(bins, y, linewidth=2, color='r') # doctest: +SKIP >>> plt.show() """ @@ -1363,11 +1365,11 @@ cdef class RandomGenerator: the probability density function: >>> import matplotlib.pyplot as plt - >>> import scipy.special as sps + >>> import scipy.special as sps # doctest: +SKIP >>> count, bins, ignored = plt.hist(s, 50, density=True) - >>> y = bins**(shape-1)*(np.exp(-bins/scale) / + >>> y = bins**(shape-1)*(np.exp(-bins/scale) / # doctest: +SKIP ... (sps.gamma(shape)*scale**shape)) - >>> plt.plot(bins, y, linewidth=2, color='r') + >>> plt.plot(bins, y, linewidth=2, color='r') # doctest: +SKIP >>> plt.show() """ @@ -1917,11 +1919,11 @@ cdef class RandomGenerator: the probability density function: >>> import matplotlib.pyplot as plt - >>> from scipy.special import i0 + >>> from scipy.special import i0 # doctest: +SKIP >>> plt.hist(s, 50, density=True) >>> x = np.linspace(-np.pi, np.pi, num=51) - >>> y = np.exp(kappa*np.cos(x-mu))/(2*np.pi*i0(kappa)) - >>> plt.plot(x, y, linewidth=2, color='r') + >>> y = np.exp(kappa*np.cos(x-mu))/(2*np.pi*i0(kappa)) # doctest: +SKIP + >>> plt.plot(x, y, linewidth=2, color='r') # doctest: +SKIP >>> plt.show() """ @@ -2199,25 +2201,25 @@ cdef class RandomGenerator: Compare the power function distribution to the inverse of the Pareto. - >>> from scipy import stats + >>> from scipy import stats # doctest: +SKIP >>> rvs = np.random.power(5, 1000000) >>> rvsp = np.random.pareto(5, 1000000) >>> xx = np.linspace(0,1,100) - >>> powpdf = stats.powerlaw.pdf(xx,5) + >>> powpdf = stats.powerlaw.pdf(xx,5) # doctest: +SKIP >>> plt.figure() >>> plt.hist(rvs, bins=50, density=True) - >>> plt.plot(xx,powpdf,'r-') + >>> plt.plot(xx,powpdf,'r-') # doctest: +SKIP >>> plt.title('np.random.power(5)') >>> plt.figure() >>> plt.hist(1./(1.+rvsp), bins=50, density=True) - >>> plt.plot(xx,powpdf,'r-') + >>> plt.plot(xx,powpdf,'r-') # doctest: +SKIP >>> plt.title('inverse of 1 + np.random.pareto(5)') >>> plt.figure() >>> plt.hist(1./(1.+rvsp), bins=50, density=True) - >>> plt.plot(xx,powpdf,'r-') + >>> plt.plot(xx,powpdf,'r-') # doctest: +SKIP >>> plt.title('inverse of stats.pareto(5)') """ @@ -3208,14 +3210,15 @@ cdef class RandomGenerator: the probability density function: >>> import matplotlib.pyplot as plt - >>> from scipy import special + >>> from scipy import special # doctest: +SKIP Truncate s values at 50 so plot is interesting: - >>> count, bins, ignored = plt.hist(s[s<50], 50, density=True) + >>> count, bins, ignored = plt.hist(s[s<50], + ... 50, density=True) >>> x = np.arange(1., 50.) - >>> y = x**(-a) / special.zetac(a) - >>> plt.plot(x, y/max(y), linewidth=2, color='r') + >>> y = x**(-a) / special.zetac(a) # doctest: +SKIP + >>> plt.plot(x, y/max(y), linewidth=2, color='r') # doctest: +SKIP >>> plt.show() """ diff --git a/numpy/random/mt19937.pyx b/numpy/random/mt19937.pyx index 306ce23e2..8ed229984 100644 --- a/numpy/random/mt19937.pyx +++ b/numpy/random/mt19937.pyx @@ -98,13 +98,13 @@ cdef class MT19937: generators should be initialized with the same seed to ensure that the segments come from the same sequence. - >>> from numpy.random.randomgen.entropy import random_entropy - >>> from numpy.random.randomgen import RandomGenerator, MT19937 + >>> from numpy.random.entropy import random_entropy + >>> from numpy.random import RandomGenerator, MT19937 >>> seed = random_entropy() - >>> rs = [RandomGenerator(MT19937(seed) for _ in range(10)] - # Advance rs[i] by i jumps + >>> rs = [RandomGenerator(MT19937(seed)) for _ in range(10)] + # Advance each MT19937 instance by i jumps >>> for i in range(10): - rs[i].jump(i) + ... rs[i].brng.jump(i) References ---------- @@ -145,8 +145,10 @@ cdef class MT19937: self.capsule = PyCapsule_New(<void *>self._brng, name, NULL) def __dealloc__(self): - free(self.rng_state) - free(self._brng) + if self.rng_state: + free(self.rng_state) + if self._brng: + free(self._brng) # Pickling support: def __getstate__(self): @@ -353,7 +355,7 @@ cdef class MT19937: Returns ------- - gen : numpy.random.randomgen.generator.RandomGenerator + gen : numpy.random.RandomGenerator Random generator used this instance as the core PRNG """ if self._generator is None: diff --git a/numpy/random/mtrand.pyx b/numpy/random/mtrand.pyx index b5d6ff9bc..145af4c7c 100644 --- a/numpy/random/mtrand.pyx +++ b/numpy/random/mtrand.pyx @@ -90,8 +90,10 @@ cdef class RandomState: self.lock = brng.lock def __dealloc__(self): - free(self._binomial) - free(self._aug_state) + if self._binomial: + free(self._binomial) + if self._aug_state: + free(self._aug_state) def __repr__(self): return self.__str__() + ' at 0x{:X}'.format(id(self)) @@ -135,7 +137,7 @@ cdef class RandomState: The best method to access seed is to directly use a basic RNG instance. This example demonstrates this best practice. - >>> from numpy.random.randomgen import MT19937 + >>> from numpy.random import MT19937 >>> from numpy.random import RandomState >>> brng = MT19937(123456789) >>> rs = RandomState(brng) @@ -479,8 +481,6 @@ cdef class RandomState: [ 739731006, 1947757578]], [[1871712945, 752307660], [1601631370, 1479324245]]]) - >>> np.iinfo(np.int).max - 2147483647 >>> rs.tomaxint((2,2,2)) < np.iinfo(np.int).max array([[[ True, True], [ True, True]], @@ -1358,11 +1358,11 @@ cdef class RandomState: the probability density function: >>> import matplotlib.pyplot as plt - >>> import scipy.special as sps + >>> import scipy.special as sps # doctest: +SKIP >>> count, bins, ignored = plt.hist(s, 50, density=True) - >>> y = bins**(shape-1) * ((np.exp(-bins/scale))/ \\ + >>> y = bins**(shape-1) * ((np.exp(-bins/scale))/ # doctest: +SKIP ... (sps.gamma(shape) * scale**shape)) - >>> plt.plot(bins, y, linewidth=2, color='r') + >>> plt.plot(bins, y, linewidth=2, color='r') # doctest: +SKIP >>> plt.show() """ @@ -1437,11 +1437,11 @@ cdef class RandomState: the probability density function: >>> import matplotlib.pyplot as plt - >>> import scipy.special as sps + >>> import scipy.special as sps # doctest: +SKIP >>> count, bins, ignored = plt.hist(s, 50, density=True) - >>> y = bins**(shape-1)*(np.exp(-bins/scale) / + >>> y = bins**(shape-1)*(np.exp(-bins/scale) / # doctest: +SKIP ... (sps.gamma(shape)*scale**shape)) - >>> plt.plot(bins, y, linewidth=2, color='r') + >>> plt.plot(bins, y, linewidth=2, color='r') # doctest: +SKIP >>> plt.show() """ @@ -1991,11 +1991,11 @@ cdef class RandomState: the probability density function: >>> import matplotlib.pyplot as plt - >>> from scipy.special import i0 + >>> from scipy.special import i0 # doctest: +SKIP >>> plt.hist(s, 50, density=True) >>> x = np.linspace(-np.pi, np.pi, num=51) - >>> y = np.exp(kappa*np.cos(x-mu))/(2*np.pi*i0(kappa)) - >>> plt.plot(x, y, linewidth=2, color='r') + >>> y = np.exp(kappa*np.cos(x-mu))/(2*np.pi*i0(kappa)) # doctest: +SKIP + >>> plt.plot(x, y, linewidth=2, color='r') # doctest: +SKIP >>> plt.show() """ @@ -2273,25 +2273,25 @@ cdef class RandomState: Compare the power function distribution to the inverse of the Pareto. - >>> from scipy import stats + >>> from scipy import stats # doctest: +SKIP >>> rvs = np.random.power(5, 1000000) >>> rvsp = np.random.pareto(5, 1000000) >>> xx = np.linspace(0,1,100) - >>> powpdf = stats.powerlaw.pdf(xx,5) + >>> powpdf = stats.powerlaw.pdf(xx,5) # doctest: +SKIP >>> plt.figure() >>> plt.hist(rvs, bins=50, density=True) - >>> plt.plot(xx,powpdf,'r-') + >>> plt.plot(xx,powpdf,'r-') # doctest: +SKIP >>> plt.title('np.random.power(5)') >>> plt.figure() >>> plt.hist(1./(1.+rvsp), bins=50, density=True) - >>> plt.plot(xx,powpdf,'r-') + >>> plt.plot(xx,powpdf,'r-') # doctest: +SKIP >>> plt.title('inverse of 1 + np.random.pareto(5)') >>> plt.figure() >>> plt.hist(1./(1.+rvsp), bins=50, density=True) - >>> plt.plot(xx,powpdf,'r-') + >>> plt.plot(xx,powpdf,'r-') # doctest: +SKIP >>> plt.title('inverse of stats.pareto(5)') """ @@ -3282,14 +3282,14 @@ cdef class RandomState: the probability density function: >>> import matplotlib.pyplot as plt - >>> from scipy import special + >>> from scipy import special # doctest: +SKIP Truncate s values at 50 so plot is interesting: >>> count, bins, ignored = plt.hist(s[s<50], 50, density=True) >>> x = np.arange(1., 50.) - >>> y = x**(-a) / special.zetac(a) - >>> plt.plot(x, y/max(y), linewidth=2, color='r') + >>> y = x**(-a) / special.zetac(a) # doctest: +SKIP + >>> plt.plot(x, y/max(y), linewidth=2, color='r') # doctest: +SKIP >>> plt.show() """ diff --git a/numpy/random/pcg32.pyx b/numpy/random/pcg32.pyx index 5f2b34807..64e79dfe1 100644 --- a/numpy/random/pcg32.pyx +++ b/numpy/random/pcg32.pyx @@ -95,15 +95,15 @@ cdef class PCG32: The preferable method is to use sub-streams, which are generated by using the same value of ``seed`` and incrementing the second value, ``inc``. - >>> from randomgen import RandomGenerator, PCG32 + >>> from numpy.random import RandomGenerator, PCG32 >>> rg = [RandomGenerator(PCG32(1234, i + 1)) for i in range(10)] - The alternative method is to call ``advance`` on a instance to - produce non-overlapping sequences. + The alternative method is to call ``advance`` with a different value on + each instance to produce non-overlapping sequences. >>> rg = [RandomGenerator(PCG32(1234, i + 1)) for i in range(10)] >>> for i in range(10): - ... rg[i].advance(i * 2**32) + ... rg[i].brng.advance(i * 2**32) **State and Seeding** @@ -160,8 +160,10 @@ cdef class PCG32: self.state) def __dealloc__(self): - free(self.rng_state) - free(self._brng) + if self.rng_state: + free(self.rng_state) + if self._brng: + free(self._brng) def random_raw(self, size=None, output=True): """ @@ -376,7 +378,7 @@ cdef class PCG32: Returns ------- - gen : numpy.random.randomgen.generator.RandomGenerator + gen : numpy.random.RandomGenerator Random generator used this instance as the core PRNG """ if self._generator is None: diff --git a/numpy/random/pcg64.pyx b/numpy/random/pcg64.pyx index f67d9623c..726bae027 100644 --- a/numpy/random/pcg64.pyx +++ b/numpy/random/pcg64.pyx @@ -104,15 +104,15 @@ cdef class PCG64: The preferable method is to use sub-streams, which are generated by using the same value of ``seed`` and incrementing the second value, ``inc``. - >>> from randomgen import RandomGenerator, PCG64 + >>> from numpy.random import RandomGenerator, PCG64 >>> rg = [RandomGenerator(PCG64(1234, i + 1)) for i in range(10)] - The alternative method is to call ``advance`` on a single instance to - produce non-overlapping sequences. + The alternative method is to call ``advance`` with a different value on + each instance to produce non-overlapping sequences. >>> rg = [RandomGenerator(PCG64(1234, i + 1)) for i in range(10)] >>> for i in range(10): - ... rg[i].advance(i * 2**64) + ... rg[i].brng.advance(i * 2**64) **State and Seeding** @@ -171,8 +171,10 @@ cdef class PCG64: self.state) def __dealloc__(self): - free(self.rng_state) - free(self._brng) + if self.rng_state: + free(self.rng_state) + if self._brng: + free(self._brng) cdef _reset_state_variables(self): self.rng_state.has_uint32 = 0 @@ -430,7 +432,7 @@ cdef class PCG64: Returns ------- - gen : numpy.random.randomgen.generator.RandomGenerator + gen : numpy.random.RandomGenerator Random generator using this instance as the core RNG """ if self._generator is None: diff --git a/numpy/random/philox.pyx b/numpy/random/philox.pyx index 70afd55ab..4ca38bb11 100644 --- a/numpy/random/philox.pyx +++ b/numpy/random/philox.pyx @@ -112,15 +112,15 @@ cdef class Philox: in parallel applications by using a sequence of distinct keys where each instance uses different key. - >>> from randomgen import RandomGenerator, Philox + >>> from numpy.random import RandomGenerator, Philox >>> rg = [RandomGenerator(Philox(1234)) for _ in range(10)] - # Advance rs[i] by i jumps + # Advance each Philox instance by i jumps >>> for i in range(10): - ... rg[i].jump(i) + ... rg[i].brng.jump(i) Using distinct keys produces independent streams - >>> key = 2**196 + 2**132 + 2**65 + 2**33 + 2**17 + 2**9 + >>> key = 2**96 + 2**32 + 2**65 + 2**33 + 2**17 + 2**9 >>> rg = [RandomGenerator(Philox(key=key+i)) for i in range(10)] **State and Seeding** @@ -144,14 +144,16 @@ cdef class Philox: Examples -------- - >>> from randomgen import RandomGenerator, Philox + >>> from numpy.random import RandomGenerator, Philox >>> rg = RandomGenerator(Philox(1234)) >>> rg.standard_normal() + 0.123 # random Identical method using only Philox >>> rg = Philox(1234).generator >>> rg.standard_normal() + 0.123 # random References ---------- @@ -205,10 +207,12 @@ cdef class Philox: self.state) def __dealloc__(self): - free(self.rng_state.ctr) - free(self.rng_state.key) - free(self.rng_state) - free(self._brng) + if self.rng_state: + free(self.rng_state.ctr) + free(self.rng_state.key) + free(self.rng_state) + if self._brng: + free(self._brng) cdef _reset_state_variables(self): self.rng_state.has_uint32 = 0 @@ -471,7 +475,7 @@ cdef class Philox: Returns ------- - gen : numpy.random.randomgen.generator.RandomGenerator + gen : numpy.random.RandomGenerator Random generator used this instance as the core PRNG """ if self._generator is None: diff --git a/numpy/random/setup.py b/numpy/random/setup.py index 088764791..38d968feb 100644 --- a/numpy/random/setup.py +++ b/numpy/random/setup.py @@ -48,7 +48,7 @@ def configuration(parent_package='',top_path=None): # Configure mtrand config.add_extension('_mtrand', sources=[join('_mtrand', x) for x in - ['mtrand.c', 'randomkit.c', 'initarray.c', + ['_mtrand.c', 'randomkit.c', 'initarray.c', 'distributions.c']]+[generate_libraries], libraries=libs, depends=[join('_mtrand', '*.h'), diff --git a/numpy/random/threefry.pyx b/numpy/random/threefry.pyx index 8140c6a9b..a8878c6bc 100644 --- a/numpy/random/threefry.pyx +++ b/numpy/random/threefry.pyx @@ -107,11 +107,11 @@ cdef class ThreeFry: in parallel applications by using a sequence of distinct keys where each instance uses different key. - >>> from randomgen import RandomGenerator, ThreeFry + >>> from numpy.random import RandomGenerator, ThreeFry >>> rg = [RandomGenerator(ThreeFry(1234)) for _ in range(10)] - # Advance rs[i] by i jumps + # Advance each ThreeFry instance by i jumps >>> for i in range(10): - ... rg[i].jump(i) + ... rg[i].brng.jump(i) Using distinct keys produces independent streams @@ -139,14 +139,16 @@ cdef class ThreeFry: Examples -------- - >>> from randomgen import RandomGenerator, ThreeFry + >>> from numpy.random import RandomGenerator, ThreeFry >>> rg = RandomGenerator(ThreeFry(1234)) >>> rg.standard_normal() + 0.123 # random Identical method using only ThreeFry >>> rg = ThreeFry(1234).generator >>> rg.standard_normal() + 0.123 # random References ---------- @@ -198,10 +200,12 @@ cdef class ThreeFry: self.state) def __dealloc__(self): - free(self.rng_state.ctr) - free(self.rng_state.key) - free(self.rng_state) - free(self._brng) + if self.rng_state: + free(self.rng_state.ctr) + free(self.rng_state.key) + free(self.rng_state) + if self._brng: + free(self._brng) cdef _reset_state_variables(self): self.rng_state.has_uint32 = 0 @@ -462,7 +466,7 @@ cdef class ThreeFry: Returns ------- - gen : numpy.random.randomgen.generator.RandomGenerator + gen : numpy.random.RandomGenerator Random generator used this instance as the core PRNG """ if self._generator is None: diff --git a/numpy/random/threefry32.pyx b/numpy/random/threefry32.pyx index 1fa98eabd..b1b4d4c51 100644 --- a/numpy/random/threefry32.pyx +++ b/numpy/random/threefry32.pyx @@ -109,11 +109,11 @@ cdef class ThreeFry32: in parallel applications by using a sequence of distinct keys where each instance uses different key. - >>> from randomgen import RandomGenerator, ThreeFry32 + >>> from numpy.random import RandomGenerator, ThreeFry32 >>> rg = [RandomGenerator(ThreeFry32(1234)) for _ in range(10)] - # Advance rs[i] by i jumps + # Advance each ThreeFry32 instance by i jumps >>> for i in range(10): - ... rg[i].jump(i) + ... rg[i].brng.jump(i) Using distinct keys produces independent streams @@ -142,14 +142,16 @@ cdef class ThreeFry32: Examples -------- - >>> from randomgen import RandomGenerator, ThreeFry32 + >>> from numpy.random import RandomGenerator, ThreeFry32 >>> rg = RandomGenerator(ThreeFry32(1234)) >>> rg.standard_normal() + 0.123 # random Identical method using only ThreeFry32 >>> rg = ThreeFry32(1234).generator >>> rg.standard_normal() + 0.123 # random References ---------- @@ -201,10 +203,12 @@ cdef class ThreeFry32: self.state) def __dealloc__(self): - free(self.rng_state.ctr) - free(self.rng_state.key) - free(self.rng_state) - free(self._brng) + if self.rng_state: + free(self.rng_state.ctr) + free(self.rng_state.key) + free(self.rng_state) + if self._brng: + free(self._brng) cdef _reset_state_variables(self): self.rng_state.buffer_pos = THREEFRY_BUFFER_SIZE @@ -458,7 +462,7 @@ cdef class ThreeFry32: Returns ------- - gen : numpy.random.randomgen.generator.RandomGenerator + gen : numpy.random.RandomGenerator Random generator used this instance as the core PRNG """ if self._generator is None: diff --git a/numpy/random/xoroshiro128.pyx b/numpy/random/xoroshiro128.pyx index 7795500e8..5c4642513 100644 --- a/numpy/random/xoroshiro128.pyx +++ b/numpy/random/xoroshiro128.pyx @@ -84,11 +84,11 @@ cdef class Xoroshiro128: in each worker process. All generators should be initialized with the same seed to ensure that the segments come from the same sequence. - >>> from randomgen import RandomGenerator, Xoroshiro128 + >>> from numpy.random import RandomGenerator, Xoroshiro128 >>> rg = [RandomGenerator(Xoroshiro128(1234)) for _ in range(10)] - # Advance rs[i] by i jumps + # Advance each Xoroshiro128 instance by i jumps >>> for i in range(10): - ... rg[i].jump(i) + ... rg[i].brng.jump(i) **State and Seeding** @@ -108,14 +108,16 @@ cdef class Xoroshiro128: Examples -------- - >>> from randomgen import RandomGenerator, Xoroshiro128 + >>> from numpy.random import RandomGenerator, Xoroshiro128 >>> rg = RandomGenerator(Xoroshiro128(1234)) >>> rg.standard_normal() + 0.123 # random Identical method using only Xoroshiro128 >>> rg = Xoroshiro128(1234).generator >>> rg.standard_normal() + 0.123 # random References ---------- @@ -163,8 +165,10 @@ cdef class Xoroshiro128: self.state) def __dealloc__(self): - free(self.rng_state) - free(self._brng) + if self.rng_state: + free(self.rng_state) + if self._brng: + free(self._brng) cdef _reset_state_variables(self): self.rng_state.has_uint32 = 0 @@ -348,7 +352,7 @@ cdef class Xoroshiro128: Returns ------- - gen : numpy.random.randomgen.generator.RandomGenerator + gen : numpy.random.RandomGenerator Random generator used this instance as the basic RNG """ if self._generator is None: diff --git a/numpy/random/xorshift1024.pyx b/numpy/random/xorshift1024.pyx index 3c7ffac52..04fb542c1 100644 --- a/numpy/random/xorshift1024.pyx +++ b/numpy/random/xorshift1024.pyx @@ -83,11 +83,11 @@ cdef class Xorshift1024: in each worker process. All generators should be initialized with the same seed to ensure that the segments come from the same sequence. - >>> from randomgen import RandomGenerator, Xorshift1024 + >>> from numpy.random import RandomGenerator, Xorshift1024 >>> rg = [RandomGenerator(Xorshift1024(1234)) for _ in range(10)] - # Advance rg[i] by i jumps + # Advance each Xorshift1024 instance by i jumps >>> for i in range(10): - ... rg[i].jump(i) + ... rg[i].brng.jump(i) **State and Seeding** @@ -107,14 +107,16 @@ cdef class Xorshift1024: Examples -------- - >>> from randomgen import RandomGenerator, Xorshift1024 + >>> from numpy.random import RandomGenerator, Xorshift1024 >>> rg = RandomGenerator(Xorshift1024(1234)) >>> rg.standard_normal() + 0.123 # random Identical method using only Xoroshiro128 - >>> rg = Xorshift10241234).generator + >>> rg = Xorshift1024(1234).generator >>> rg.standard_normal() + 0.123 # random References ---------- @@ -169,8 +171,10 @@ cdef class Xorshift1024: self.state) def __dealloc__(self): - free(self.rng_state) - free(self._brng) + if self.rng_state: + free(self.rng_state) + if self._brng: + free(self._brng) cdef _reset_state_variables(self): self.rng_state.has_uint32 = 0 @@ -356,7 +360,7 @@ cdef class Xorshift1024: Returns ------- - gen : numpy.random.randomgen.generator.RandomGenerator + gen : numpy.random.RandomGenerator Random generator used this instance as the core PRNG """ if self._generator is None: diff --git a/numpy/random/xoshiro256starstar.pyx b/numpy/random/xoshiro256starstar.pyx index c3856b6f7..278356b05 100644 --- a/numpy/random/xoshiro256starstar.pyx +++ b/numpy/random/xoshiro256starstar.pyx @@ -83,11 +83,11 @@ cdef class Xoshiro256StarStar: in each worker process. All generators should be initialized with the same seed to ensure that the segments come from the same sequence. - >>> from randomgen import RandomGenerator, Xoshiro256StarStar + >>> from numpy.random import RandomGenerator, Xoshiro256StarStar >>> rg = [RandomGenerator(Xoshiro256StarStar(1234)) for _ in range(10)] - # Advance rs[i] by i jumps + # Advance each Xoshiro256StarStar instance by i jumps >>> for i in range(10): - ... rg[i].jump(i) + ... rg[i].brng.jump(i) **State and Seeding** @@ -108,14 +108,16 @@ cdef class Xoshiro256StarStar: Examples -------- - >>> from randomgen import RandomGenerator, Xoshiro256StarStar + >>> from numpy.random import RandomGenerator, Xoshiro256StarStar >>> rg = RandomGenerator(Xoshiro256StarStar(1234)) >>> rg.standard_normal() + 0.123 # random Identical method using only Xoshiro256StarStar >>> rg = Xoshiro256StarStar(1234).generator >>> rg.standard_normal() + 0.123 # random References ---------- @@ -163,8 +165,10 @@ cdef class Xoshiro256StarStar: self.state) def __dealloc__(self): - free(self.rng_state) - free(self._brng) + if self.rng_state: + free(self.rng_state) + if self._brng: + free(self._brng) cdef _reset_state_variables(self): self.rng_state.has_uint32 = 0 @@ -353,7 +357,7 @@ cdef class Xoshiro256StarStar: Returns ------- - gen : numpy.random.randomgen.generator.RandomGenerator + gen : numpy.random.RandomGenerator Random generator used this instance as the basic RNG """ if self._generator is None: diff --git a/numpy/random/xoshiro512starstar.pyx b/numpy/random/xoshiro512starstar.pyx index 761d1f1d0..65d4c7c3e 100644 --- a/numpy/random/xoshiro512starstar.pyx +++ b/numpy/random/xoshiro512starstar.pyx @@ -83,11 +83,11 @@ cdef class Xoshiro512StarStar: in each worker process. All generators should be initialized with the same seed to ensure that the segments come from the same sequence. - >>> from randomgen import RandomGenerator, Xoshiro512StarStar + >>> from numpy.random import RandomGenerator, Xoshiro512StarStar >>> rg = [RandomGenerator(Xoshiro512StarStar(1234)) for _ in range(10)] - # Advance rs[i] by i jumps + # Advance each Xoshiro512StarStar instance by i jumps >>> for i in range(10): - ... rg[i].jump(i) + ... rg[i].brng.jump(i) **State and Seeding** @@ -108,14 +108,16 @@ cdef class Xoshiro512StarStar: Examples -------- - >>> from randomgen import RandomGenerator, Xoshiro512StarStar + >>> from numpy.random import RandomGenerator, Xoshiro512StarStar >>> rg = RandomGenerator(Xoshiro512StarStar(1234)) >>> rg.standard_normal() + 0.123 # random Identical method using only Xoshiro512StarStar >>> rg = Xoshiro512StarStar(1234).generator >>> rg.standard_normal() + 0.123 # random References ---------- @@ -163,8 +165,10 @@ cdef class Xoshiro512StarStar: self.state) def __dealloc__(self): - free(self.rng_state) - free(self._brng) + if self.rng_state: + free(self.rng_state) + if self._brng: + free(self._brng) cdef _reset_state_variables(self): self.rng_state.has_uint32 = 0 @@ -347,7 +351,7 @@ cdef class Xoshiro512StarStar: Returns ------- - gen : numpy.random.randomgen.generator.RandomGenerator + gen : numpy.random.RandomGenerator Random generator used this instance as the basic RNG """ if self._generator is None: |
