Skip to content

Commit

Permalink
ENH: Add closed generator to randint
Browse files Browse the repository at this point in the history
Add closed option to randint to simplify some cases
  • Loading branch information
bashtage authored and mattip committed May 20, 2019
1 parent ca9c542 commit dd77ce3
Show file tree
Hide file tree
Showing 11 changed files with 191 additions and 273 deletions.
2 changes: 1 addition & 1 deletion doc/source/about.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ needed for scientific computing with Python. This package contains:
- sophisticated :ref:`(broadcasting) functions <ufuncs>`
- basic :ref:`linear algebra functions <routines.linalg>`
- basic :ref:`Fourier transforms <routines.fft>`
- sophisticated :ref:`random number capabilities <routines.random>`
- sophisticated :ref:`random number capabilities <numpyrandom>`
- tools for integrating Fortran code
- tools for integrating C/C++ code

Expand Down
4 changes: 0 additions & 4 deletions doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,3 @@ def linkcode_resolve(domain, info):
return "https://github.com/numpy/numpy/blob/v%s/numpy/%s%s" % (
numpy.__version__, fn, linespec)

doctest_global_setup = '''
import numpy as np
from numpy.random import randomgen
'''
66 changes: 2 additions & 64 deletions doc/source/reference/random/index.rst
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
.. _numpyrandom:

.. currentmodule:: numpy.random

numpy.random
============

<<<<<<< HEAD
A `~RandomGenerator` can
be initialized with a number of different Random Number Generators (RNG)s, and
exposes many different probability distributions.

=======
Numpy's random number routines produce psuedo random numbers using
combinations of a `BitGenerator` to create sequences and a `Generator`
to use those sequences to sample from different statistical distributions:
Expand All @@ -30,37 +26,23 @@ available, but limited to a single BitGenerator.
For convenience and backward compatibility, a single `RandomState`
instance's methods are imported into the numpy.random namespace, see
:ref:`legacy` for the complete list.
>>>>>>> d7650d9f2... DOC: use more BitGenerator instead of other termonology

Quick Start
-----------

<<<<<<< HEAD
By default, `RandomGenerator` uses normals provided by
`xoroshiro128.Xoroshiro128` which will be faster than the legacy methods in
`mtrand.RandomState`
=======
By default, `Generator` uses normals provided by `xoroshiro128.Xoroshiro128`
which will be faster than the legacy methods in `RandomState`
>>>>>>> d7650d9f2... DOC: use more BitGenerator instead of other termonology

.. code-block:: python
# Uses the old numpy.random.RandomState
from numpy import random
random.standard_normal()
<<<<<<< HEAD
`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``.
=======
`Generator` can be used as a direct replacement for `RandomState`,
although the random values are generated by `~xoroshiro128.Xoroshiro128`. The
`Generator` holds an instance of a BitGenerator. It is accessable as
``gen.bit_generator``.
>>>>>>> d7650d9f2... DOC: use more BitGenerator instead of other termonology

.. code-block:: python
Expand All @@ -85,49 +67,23 @@ generator`.
Introduction
------------
RandomGen takes a different approach to producing random numbers from the
<<<<<<< HEAD
:class:`numpy.random.RandomState` object. Random number generation is
separated into two components, a basic RNG and a random generator.
=======
`RandomState` object. Random number generation is separated into two
components, a bit generator and a random generator.
>>>>>>> d7650d9f2... DOC: use more BitGenerator instead of other termonology

The basic RNG has a limited set of responsibilities. It manages the
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.

<<<<<<< HEAD
The `random generator <~RandomGenerator>` takes the
basic RNG-provided functions and transforms them into more useful
=======
The `random generator <Generator>` takes the
bit generator-provided stream and transforms them into more useful
>>>>>>> d7650d9f2... DOC: use more BitGenerator instead of other termonology
distributions, e.g., simulated normal random values. This structure allows
alternative basic RNGs to be used without code duplication.

<<<<<<< HEAD
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 numpy.random import RandomGenerator, MT19937
rg = RandomGenerator(MT19937())
rg.random_sample()
Seed information is directly passed to the basic RNG.
=======
The `Generator` is the user-facing object that is nearly identical to
`RandomState`. The canonical method to initialize a generator passes a
`~mt19937.MT19937` bit generator, the underlying bit generator in Python -- as
the sole argument. Note that the bit generator must be instantiated.
>>>>>>> d7650d9f2... DOC: use more BitGenerator instead of other termonology

.. code-block:: python
Expand All @@ -147,13 +103,8 @@ What's New or Different
.. warning::

The Box-Muller method used to produce NumPy's normals is no longer available
<<<<<<< HEAD
in `~.RandomGenerator`. It is not possible to reproduce the exact random
values using ``RandomGenerator`` for the normal distribution or any other
=======
in `Generator`. It is not possible to reproduce the exact random
values using Generator for the normal distribution or any other
>>>>>>> d7650d9f2... DOC: use more BitGenerator instead of other termonology
distribution that relies on the normal such as the `numpy.random.gamma` or
`numpy.random.standard_t`. If you require bitwise backward compatible
streams, use `RandomState`.
Expand All @@ -169,17 +120,6 @@ What's New or Different
* `~entropy.random_entropy` provides access to the system
source of randomness that is used in cryptographic applications (e.g.,
``/dev/urandom`` on Unix).
<<<<<<< HEAD
* All basic random generators functions can produce doubles, uint64s and
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
`~RandomGenerator.randint`.
=======
* All BitGenerators can produce doubles, uint64s and uint32s via CTypes
(`~xoroshiro128.Xoroshiro128.ctypes`) and CFFI
(:meth:`~xoroshiro128.Xoroshiro128.cffi`). This allows the bit generators to
Expand All @@ -195,8 +135,6 @@ What's New or Different
random numbers, which replaces `random_sample`, `sample`, and `ranf`. This
is consistent with Python's `random.random`.

>>>>>>> d7650d9f2... DOC: use more BitGenerator instead of other termonology

See :ref:`new-or-different` for a complete list of improvements and
differences.

Expand Down
83 changes: 0 additions & 83 deletions doc/source/reference/routines.random.rst

This file was deleted.

1 change: 0 additions & 1 deletion doc/source/reference/routines.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ indentation.
routines.other
routines.padding
routines.polynomials
routines.random
random/index
routines.set
routines.sort
Expand Down
2 changes: 1 addition & 1 deletion numpy/random/bounded_integers.pxd.in
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ py:
inttypes = ('uint64','uint32','uint16','uint8','bool','int64','int32','int16','int8')
}}
{{for inttype in inttypes}}
cdef object _rand_{{inttype}}(object low, object high, object size, bint use_masked, brng_t *state, object lock)
cdef object _rand_{{inttype}}(object low, object high, object size, bint use_masked, bint closed, brng_t *state, object lock)
{{endfor}}
Loading

0 comments on commit dd77ce3

Please sign in to comment.