Skip to content

Commit 080a9a0

Browse files
authored
Merge pull request #95 from bashtage/fix-advance-docs
DOC: Update advance documentation
2 parents 67354da + 7675169 commit 080a9a0

File tree

3 files changed

+66
-11
lines changed

3 files changed

+66
-11
lines changed

doc/source/multithreading.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ which is fast, has a long period and supports using ``jump`` to advance the
1414
state. The random numbers generated are reproducible in the sense that the
1515
same seed will produce the same outputs.
1616

17-
::
17+
.. code-block:: python
1818
1919
import randomstate
2020
import multiprocessing

doc/source/parallel.rst

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ should be used. This example shows how many streams can be created by
1616
passing in different index values in the second input while using the
1717
same seed in the first.
1818

19-
::
19+
.. code-block:: python
2020
2121
from randomstate.entropy import random_entropy
2222
import randomstate.prng.pcg64 as pcg64
@@ -32,6 +32,9 @@ same seed in the first.
3232
Jump/Advance the PRNG state
3333
---------------------------
3434

35+
Jump
36+
****
37+
3538
``jump`` advances the state of the PRNG *as-if* a large number of random
3639
numbers have been drawn. The specific number of draws varies by PRNG, and
3740
ranges from :math:`2^{64}` to :math:`2^{512}`. Additionally, the *as-if*
@@ -55,7 +58,7 @@ are listed below.
5558
``jump`` can be used to produce long blocks which should be long enough to not
5659
overlap.
5760

58-
::
61+
.. code-block:: python
5962
6063
from randomstate.entropy import random_entropy
6164
import randomstate.prng.xorshift1024 as xorshift1024
@@ -70,7 +73,47 @@ overlap.
7073
blocks.append(block)
7174
7275
76+
Advance
77+
*******
7378
``advance`` can be used to jump the state an arbitrary number of steps, and so
7479
is a more general approach than ``jump``. Only ``pcg32`` and ``pcg64``
7580
support ``advance``, and since these also support independent streams, it is
7681
not usually necessary to use ``advance``.
82+
83+
Advancing a PRNG updates the underlying PRNG state as-if a given number of
84+
calls to the underlying PRNG have been made. In general there is not a
85+
one-to-one relationship between the number output random values from a
86+
particular distribution and the number of draws from the core PRNG.
87+
This occurs for two reasons:
88+
89+
* The random values are simulated using a rejection-based method
90+
and so, on average, more than one value from the underlying
91+
PRNG is required to generate an single draw.
92+
* The number of bits required to generate a simulated value
93+
differs from the number of bits generated by the underlying
94+
PRNG. For example, two 16-bit integer values can be simulated
95+
from a single draw of a 32-bit PRNG.
96+
97+
Advancing the PRNG state resets any pre-computed random numbers. This is
98+
required to ensure exact reproducibility.
99+
100+
This example uses ``advance`` to advance a ``pcg64`` generator 2 ** 127
101+
steps to set a sequence of random number generators.
102+
103+
.. code-block:: python
104+
105+
import randomstate.prng.pcg64 as pcg64
106+
rs = pcg64.RandomState()
107+
rs_gen = pcg64.RandomState()
108+
rs_gen.set_state(rs.get_state())
109+
110+
advance = 2**127
111+
rngs = [rs]
112+
for _ in range(9):
113+
rs_gen.advance(advance)
114+
rng = pcg64.RandomState()
115+
rng.set_state(rs_gen.get_state())
116+
rngs.append(rng)
117+
118+
.. end block
119+

randomstate/randomstate.pyx

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -417,12 +417,13 @@ cdef class RandomState:
417417
"""
418418
advance(delta)
419419
420-
Advance the PRNG as-if delta drawn have occurred.
420+
Advance the underlying PRNG as-if delta draws have occurred.
421421
422422
Parameters
423423
----------
424424
delta : integer, positive
425-
Number of draws to advance the PRNG.
425+
Number of draws to advance the PRNG. Must be less than the
426+
size state variable in the underlying PRNG.
426427
427428
Returns
428429
-------
@@ -431,7 +432,21 @@ cdef class RandomState:
431432
432433
Notes
433434
-----
434-
Advancing the prng state resets any pre-computed random numbers.
435+
Advancing a PRNG updates the underlying PRNG state as-if a given
436+
number of calls to the underlying PRNG have been made. In general
437+
there is not a one-to-one relationship between the number output
438+
random values from a particular distribution and the number of
439+
draws from the core PRNG. This occurs for two reasons:
440+
441+
* The random values are simulated using a rejection-based method
442+
and so, on average, more than one value from the underlying
443+
PRNG is required to generate an single draw.
444+
* The number of bits required to generate a simulated value
445+
differs from the number of bits generated by the underlying
446+
PRNG. For example, two 16-bit integer values can be simulated
447+
from a single draw of a 32-bit PRNG.
448+
449+
Advancing the PRNG state resets any pre-computed random numbers.
435450
This is required to ensure exact reproducibility.
436451
"""
437452
IF RS_RNG_MOD_NAME == 'pcg64':
@@ -1690,13 +1705,10 @@ cdef class RandomState:
16901705
def complex_normal(self, loc=0.0, gamma=1.0, relation=0.0, size=None,
16911706
method=__normal_method):
16921707
"""
1693-
normal(loc=0.0, gamma=1.0, relation=0.0, size=None, method='bm')
1708+
complex_normal(loc=0.0, gamma=1.0, relation=0.0, size=None, method='bm')
16941709
16951710
Draw random samples from a complex normal (Gaussian) distribution.
16961711
1697-
The complex normal is closely related to a bivariate normal
1698-
distribution where one of the dimensions is complex.
1699-
17001712
Parameters
17011713
----------
17021714
loc : complex or array_like of complex
@@ -1729,7 +1741,7 @@ cdef class RandomState:
17291741
Notes
17301742
-----
17311743
**EXPERIMENTAL** Not part of official NumPy RandomState, may change until
1732-
formal release on PyPi
1744+
formal release on PyPi.
17331745
17341746
Complex normals are generated from a bivariate normal where the
17351747
variance of the real component is 0.5 Re(gamma + relation), the

0 commit comments

Comments
 (0)