Skip to content

Commit c9c9935

Browse files
Sheppard, KevinSheppard, Kevin
authored andcommitted
FIX: Add jump and advance to module base
Add jump and advance to module base where available Improve doc strings to explain how to use PRNG in parallel Rename C-functions advnace and jump to avoid conflicts
1 parent c1d8618 commit c9c9935

20 files changed

+105
-26
lines changed

randomstate/defaults.pxi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
DEF RS_NORMAL_METHOD = 'zig'
2+
DEF RS_RNG_SEED = 1
3+
DEF RS_RNG_ADVANCEABLE = 0
4+
DEF RS_RNG_JUMPABLE = 0
5+
DEF RS_RNG_STATE_LEN = 4

randomstate/interface.pyx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,11 @@ cdef class RandomState:
316316
"""
317317
IF RNG_NAME == 'pcg64':
318318
IF PCG128_EMULATED:
319-
advance(&self.rng_state, pcg128_from_pylong(delta))
319+
advance_state(&self.rng_state, pcg128_from_pylong(delta))
320320
ELSE:
321-
advance(&self.rng_state, delta)
321+
advance_state(&self.rng_state, delta)
322322
ELSE:
323-
advance(&self.rng_state, delta)
323+
advance_state(&self.rng_state, delta)
324324

325325
self.rng_state.has_gauss = 0
326326
self.rng_state.gauss = 0.0
@@ -351,7 +351,7 @@ cdef class RandomState:
351351
"""
352352
cdef Py_ssize_t i;
353353
for i in range(iter):
354-
jump(&self.rng_state)
354+
jump_state(&self.rng_state)
355355
self.rng_state.has_gauss = 0
356356
self.rng_state.gauss = 0.0
357357
return None
@@ -4225,4 +4225,9 @@ dirichlet = _rand.dirichlet
42254225
shuffle = _rand.shuffle
42264226
permutation = _rand.permutation
42274227

4228-
sample = ranf = random = random_sample
4228+
sample = ranf = random = random_sample
4229+
4230+
IF RNG_JUMPABLE:
4231+
jump = _rand.jump
4232+
IF RNG_ADVANCEABLE:
4233+
advance = _rand.advance

randomstate/shims/dSFMT/dSFMT.pxi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ value is generated and returned. If `size` is an integer, then a 1-D
9292
array filled with generated values is returned. If `size` is a tuple,
9393
then an array with that shape is filled and returned.
9494
95-
*No Compatibility Guarantee*
95+
**No Compatibility Guarantee**
96+
9697
'dSFMT.RandomState' does not make a guarantee that a fixed seed and a
9798
fixed series of calls to 'dSFMT.RandomState' methods using the same
9899
parameters will always produce the same results. This is different from

randomstate/shims/mlfg-1279-861/mlfg-1279-861.pxi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ value is generated and returned. If `size` is an integer, then a 1-D
7171
array filled with generated values is returned. If ``size`` is a tuple,
7272
then an array with that shape is filled and returned.
7373
74-
*No Compatibility Guarantee*
74+
**No Compatibility Guarantee**
75+
7576
``mlfg_1279_861.RandomState`` does not make a guarantee that a fixed seed and a
7677
fixed series of calls to 'mlfg_1279_861.RandomState' methods using the same
7778
parameters will always produce the same results. This is different from

randomstate/shims/mrg32k3a/mrg32k3a.pxi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ value is generated and returned. If `size` is an integer, then a 1-D
6363
array filled with generated values is returned. If `size` is a tuple,
6464
then an array with that shape is filled and returned.
6565
66-
*No Compatibility Guarantee*
66+
**No Compatibility Guarantee**
67+
6768
``mrg32k3a.RandomState`` does not make a guarantee that a fixed seed and a
6869
fixed series of calls to ``mrg32k3a.RandomState`` methods using the same
6970
parameters will always produce the same results. This is different from

randomstate/shims/pcg-32/pcg-32-shim.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ extern inline uint64_t random_uint64(aug_state* state);
66

77
extern inline void set_seed(aug_state* state, uint64_t seed, uint64_t inc);
88

9-
extern inline void advance(aug_state* state, uint64_t delta);
9+
extern inline void advance_state(aug_state* state, uint64_t delta);
1010

1111
extern inline void entropy_init(aug_state* state);
1212

randomstate/shims/pcg-32/pcg-32-shim.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ inline void set_seed(aug_state* state, uint64_t seed, uint64_t inc)
3232
pcg32_srandom_r(state->rng, seed, inc);
3333
}
3434

35-
inline void advance(aug_state* state, uint64_t delta)
35+
inline void advance_state(aug_state* state, uint64_t delta)
3636
{
3737
pcg32_advance_r(state->rng, delta);
3838
}

randomstate/shims/pcg-32/pcg-32.pxi

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ cdef extern from "distributions.h":
2525

2626
cdef void set_seed(aug_state* state, uint64_t seed, uint64_t inc)
2727

28-
cdef void advance(aug_state* state, uint64_t delta)
28+
cdef void advance_state(aug_state* state, uint64_t delta)
2929

3030
ctypedef uint64_t rng_state_t
3131

@@ -55,7 +55,8 @@ value is generated and returned. If `size` is an integer, then a 1-D
5555
array filled with generated values is returned. If `size` is a tuple,
5656
then an array with that shape is filled and returned.
5757
58-
*No Compatibility Guarantee*
58+
**No Compatibility Guarantee**
59+
5960
``pcg32.RandomState`` does not make a guarantee that a fixed seed and a
6061
fixed series of calls to ``pcg32.RandomState`` methods using the same
6162
parameters will always produce the same results. This is different from
@@ -84,6 +85,26 @@ The state of the PCG-32 PRNG is represented by 2 64-bit unsigned integers.
8485
8586
See pcg64 for a similar implementation with a larger period.
8687
88+
** Parallel Features **
89+
90+
``pcg32.RandomState`` can be used in parallel applications in one of two ways.
91+
The preferable method is to use sub-streams, which are generated by using the
92+
same value in the first position of the seed and incrementing the second value.
93+
94+
>>> import randomstate.prng.pcg32 as rnd
95+
>>> rs = [rng.RandomState(1234, i + 1) for i in range(10)]
96+
97+
The alternative method is to use a single RandomState and advance to get
98+
non-overlapping sequences.
99+
100+
>>> import randomstate.prng.pcg32 as rnd
101+
>>> rs = [rng.RandomState(1234, 1) for _ in range(10)]
102+
>>> for i in range(10):
103+
rs[i].advance(2**32)
104+
105+
*Note*: Due to the limited period of ``pcg32.RandomState`` using ``advance``
106+
in parallel applications is not recommended.
107+
87108
References
88109
----------
89110
.. [1] "PCG, A Family of Better Random Number Generators",

randomstate/shims/pcg-64/pcg-64-docstring.pxi

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ value is generated and returned. If `size` is an integer, then a 1-D
1515
array filled with generated values is returned. If `size` is a tuple,
1616
then an array with that shape is filled and returned.
1717
18-
*No Compatibility Guarantee*
18+
**No Compatibility Guarantee**
19+
1920
``pcg64.RandomState`` does not make a guarantee that a fixed seed and a
2021
fixed series of calls to ``pcg64.RandomState`` methods using the same
2122
parameters will always produce the same results. This is different from
@@ -44,6 +45,23 @@ The state of the PCG-64 PRNG is represented by 2 128-bit unsigned integers.
4445
4546
See pcg32 for a similar implementation with a smaller period.
4647
48+
** Parallel Features **
49+
50+
``pcg64.RandomState`` can be used in parallel applications in one of two ways.
51+
The preferable method is to use sub-streams, which are generated by using the
52+
same value in the first position of the seed and incrementing the second value.
53+
54+
>>> import randomstate.prng.pcg64 as rnd
55+
>>> rs = [rng.RandomState(1234, i + 1) for i in range(10)]
56+
57+
The alternative method is to use a single RandomState and advance to get
58+
non-overlapping sequences.
59+
60+
>>> import randomstate.prng.pcg64 as rnd
61+
>>> rs = [rng.RandomState(1234, 1) for _ in range(10)]
62+
>>> for i in range(10):
63+
rs[i].advance(2**32)
64+
4765
References
4866
----------
4967
.. [1] "PCG, A Family of Better Random Number Generators",

randomstate/shims/pcg-64/pcg-64-emulated.pxi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ cdef extern from "distributions.h":
3131

3232
ctypedef s_aug_state aug_state
3333

34-
cdef void advance(aug_state* state, pcg128_t delta)
34+
cdef void advance_state(aug_state* state, pcg128_t delta)
3535

3636
cdef void set_seed(aug_state* state, pcg128_t seed, pcg128_t inc)
3737

0 commit comments

Comments
 (0)