You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lib/node_modules/@stdlib/random/base/pcg32/README.md
+15-39Lines changed: 15 additions & 39 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ limitations under the License.
20
20
21
21
# PCG32
22
22
23
-
> A linear congruential pseudorandom number generator ([LCG][lcg]) based on Park and Miller.
23
+
> A permuted congruential pseudorandom number generator ([PCG][pcg]) based on O’Neill.
24
24
25
25
<sectionclass="usage">
26
26
@@ -32,7 +32,7 @@ var pcg32 = require( '@stdlib/random/base/pcg32' );
32
32
33
33
#### pcg32()
34
34
35
-
Returns a pseudorandom integer on the interval `[1, 2147483646]`.
35
+
Returns a pseudorandom integer on the interval `[0, 4294967295]`.
36
36
37
37
```javascript
38
38
var r =pcg32();
@@ -50,7 +50,7 @@ var r = pcg32.normalized();
50
50
51
51
#### pcg32.factory( \[options] )
52
52
53
-
Returns a linear congruential pseudorandom number generator ([LCG][lcg]).
53
+
Returns a permuted congruential pseudorandom number generator ([PCG][pcg]).
54
54
55
55
```javascript
56
56
var rand =pcg32.factory();
@@ -59,10 +59,10 @@ var rand = pcg32.factory();
59
59
The function accepts the following `options`:
60
60
61
61
-**seed**: pseudorandom number generator seed.
62
-
-**state**: an [`Int32Array`][@stdlib/array/int32] containing pseudorandom number generator state. If provided, the function ignores the `seed` option.
62
+
-**state**: an [`Uint32Array`][@stdlib/array/uint32] containing pseudorandom number generator state. If provided, the function ignores the `seed` option.
63
63
-**copy**: `boolean` indicating whether to copy a provided pseudorandom number generator state. Setting this option to `false` allows sharing state between two or more pseudorandom number generators. Setting this option to `true` ensures that a returned generator has exclusive control over its internal state. Default: `true`.
64
64
65
-
By default, a random integer is used to seed the returned generator. To seed the generator, provide either an `integer` on the interval `[1, 2147483646]`
65
+
By default, a random integer is used to seed the returned generator. To seed the generator, provide either an `integer` on the interval `[0, 4294967295]`
66
66
67
67
```javascript
68
68
var rand =pcg32.factory({
@@ -73,13 +73,13 @@ var r = rand();
73
73
// returns 2557507945
74
74
```
75
75
76
-
or, for arbitrary length seeds, an array-like `object` containing signed 32-bit integers
76
+
or, for arbitrary length seeds, an array-like `object` containing unsigned 32-bit integers
77
77
78
78
```javascript
79
-
varInt32Array=require( '@stdlib/array/int32' );
79
+
varUint32Array=require( '@stdlib/array/uint32' );
80
80
81
81
var rand =pcg32.factory({
82
-
'seed':newInt32Array( [ 1234 ] )
82
+
'seed':newUint32Array( [ 1234 ] )
83
83
});
84
84
85
85
var r =rand();
@@ -234,8 +234,8 @@ var o = pcg32.toJSON();
234
234
235
235
## Notes
236
236
237
-
- The generator has a period of approximately `2.1e9` (see [Numerical Recipes in C, 2nd Edition](#references), p. 279).
238
-
-An [LCG][lcg] is fastand uses little memory. On the other hand, because the generator is a simple [linear congruential generator][lcg], the generator has recognized shortcomings. By today's PRNG standards, the generator's period is relatively short. More importantly, the "randomness quality" of the generator's output is lacking. These defects make the generator unsuitable, for example, in Monte Carlo simulations and in cryptographic applications. For more on the advantages and disadvantages of [LCGs][lcg], see [Wikipedia][pros-cons].
237
+
- The generator has a period of `2^64`.
238
+
-A [PCG][pcg] is fast, compact, and has excellent statistical quality compared to classic [LCG][lcg]s. It uses a simple [LCG][lcg] under the hood but applies permutation steps to improve output randomness. While it's not cryptographically secure, it's well-suited for most non-crypto tasks like simulations or procedural generation.
239
239
- If PRNG state is "shared" (meaning a state array was provided during PRNG creation and **not** copied) and one sets the generator state to a state array having a different length, the PRNG does **not** update the existing shared state and, instead, points to the newly provided state array. In order to synchronize PRNG output according to the new shared state array, the state array for **each** relevant PRNG must be **explicitly** set.
240
240
- If PRNG state is "shared" and one sets the generator state to a state array of the same length, the PRNG state is updated (along with the state of all other PRNGs sharing the PRNG's state array).
241
241
@@ -286,8 +286,7 @@ for ( i = 0; i < 100; i++ ) {
286
286
287
287
## References
288
288
289
-
- Park, S. K., and K. W. Miller. 1988. "Random Number Generators: Good Ones Are Hard to Find." _Communications of the ACM_ 31 (10). New York, NY, USA: ACM: 1192–1201. doi:[10.1145/63039.63042][@park:1988].
290
-
- Press, William H., Brian P. Flannery, Saul A. Teukolsky, and William T. Vetterling. 1992. _Numerical Recipes in C: The Art of Scientific Computing, Second Edition_. Cambridge University Press.
289
+
- O’Neill, Melissa E. 2014. “PCG: A Family of Simple Fast Space-Efficient Statistically Good Algorithms for Random Number Generation.” Technical Report HMC-CS-2014-0905. Harvey Mudd College, Claremont, CA. [https://www.cs.hmc.edu/tr/hmc-cs-2014-0905.pdf][@oneill:2014].
291
290
292
291
</section>
293
292
@@ -297,17 +296,6 @@ for ( i = 0; i < 100; i++ ) {
297
296
298
297
<sectionclass="related">
299
298
300
-
* * *
301
-
302
-
## See Also
303
-
304
-
- <spanclass="package-name">[`@stdlib/random/array/pcg32`][@stdlib/random/array/pcg32]</span><spanclass="delimiter">: </span><spanclass="description">create an array containing pseudorandom numbers generated using a linear congruential pseudorandom number generator (LCG).</span>
305
-
- <spanclass="package-name">[`@stdlib/random/iter/pcg32`][@stdlib/random/iter/pcg32]</span><spanclass="delimiter">: </span><spanclass="description">create an iterator for a linear congruential pseudorandom number generator (LCG) based on Park and Miller.</span>
306
-
- <spanclass="package-name">[`@stdlib/random/streams/pcg32`][@stdlib/random/streams/pcg32]</span><spanclass="delimiter">: </span><spanclass="description">create a readable stream for a linear congruential pseudorandom number generator (LCG) based on Park and Miller.</span>
307
-
- <spanclass="package-name">[`@stdlib/random/base/pcg32-shuffle`][@stdlib/random/base/pcg32-shuffle]</span><spanclass="delimiter">: </span><spanclass="description">A linear congruential pseudorandom number generator (LCG) whose output is shuffled.</span>
308
-
- <spanclass="package-name">[`@stdlib/random/base/mt19937`][@stdlib/random/base/mt19937]</span><spanclass="delimiter">: </span><spanclass="description">A 32-bit Mersenne Twister pseudorandom number generator.</span>
309
-
- <spanclass="package-name">[`@stdlib/random/base/randi`][@stdlib/random/base/randi]</span><spanclass="delimiter">: </span><spanclass="description">pseudorandom numbers having integer values.</span>
310
-
311
299
</section>
312
300
313
301
<!-- /.related -->
@@ -316,28 +304,16 @@ for ( i = 0; i < 100; i++ ) {
Copy file name to clipboardExpand all lines: lib/node_modules/@stdlib/random/base/pcg32/docs/types/index.d.ts
+10-10Lines changed: 10 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -95,28 +95,28 @@ interface PRNG {
95
95
}
96
96
97
97
/**
98
-
* Interface for generating pseudorandom integers on the interval `[1, 2147483646]`.
98
+
* Interface for generating pseudorandom integers on the interval `[0, 4294967295]`.
99
99
*/
100
100
interfaceNullaryFunctionextendsPRNG{
101
101
/**
102
-
* Returns a pseudorandom integer on the interval `[1, 2147483646]`.
102
+
* Returns a pseudorandom integer on the interval `[0, 4294967295]`.
103
103
*
104
104
* @returns pseudorandom number
105
105
*/
106
106
(): number;
107
107
}
108
108
109
109
/**
110
-
* Interface for generating pseudorandom integers on the interval `[1, 2147483646]`.
110
+
* Interface for generating pseudorandom integers on the interval `[0, 4294967295]`.
111
111
*/
112
112
interfaceRandomextendsPRNG{
113
113
/**
114
-
* Returns a pseudorandom integer on the interval `[1, 2147483646]`.
114
+
* Returns a pseudorandom integer on the interval `[0, 4294967295]`.
115
115
*
116
116
* ## Notes
117
117
*
118
-
* - This pseudorandom number generator (PRNG) is a linear congruential pseudorandom number generator (LCG) based on Park and Miller.
119
-
* - The generator has a period of approximately `2.1e9`.
118
+
* - This pseudorandom number generator (PRNG) is a permuted congruential pseudorandom number generator (PCG) based on O’Neill.
119
+
* - The generator has a period of `2^64`.
120
120
* - An LCG is fast and uses little memory. On the other hand, because the generator is a simple LCG, the generator has recognized shortcomings. By today's PRNG standards, the generator's period is relatively short. More importantly, the "randomness quality" of the generator's output is lacking. These defects make the generator unsuitable, for example, in Monte Carlo simulations and in cryptographic applications.
121
121
*
122
122
* @returns pseudorandom number
@@ -139,7 +139,7 @@ interface Random extends PRNG {
139
139
normalized(): number;
140
140
141
141
/**
142
-
* Returns a linear congruential pseudorandom number generator (LCG).
142
+
* Returns a permuted congruential pseudorandom number generator (PCG).
143
143
*
144
144
* @param options - function options
145
145
* @param options.seed - pseudorandom number generator seed
@@ -164,12 +164,12 @@ interface Random extends PRNG {
164
164
}
165
165
166
166
/**
167
-
* Returns a pseudorandom integer on the interval `[1, 2147483646]`.
167
+
* Returns a pseudorandom integer on the interval `[0, 4294967295]`.
168
168
*
169
169
* ## Notes
170
170
*
171
-
* - This pseudorandom number generator (PRNG) is a linear congruential pseudorandom number generator (LCG) based on Park and Miller.
172
-
* - The generator has a period of approximately `2.1e9`.
171
+
* - This pseudorandom number generator (PRNG) is a permuted congruential pseudorandom number generator (PCG) based on O’Neill.
172
+
* - The generator has a period of `2^64`.
173
173
* - An LCG is fast and uses little memory. On the other hand, because the generator is a simple LCG, the generator has recognized shortcomings. By today's PRNG standards, the generator's period is relatively short. More importantly, the "randomness quality" of the generator's output is lacking. These defects make the generator unsuitable, for example, in Monte Carlo simulations and in cryptographic applications.
Copy file name to clipboardExpand all lines: lib/node_modules/@stdlib/random/base/pcg32/lib/main.js
+19-27Lines changed: 19 additions & 27 deletions
Original file line number
Diff line number
Diff line change
@@ -27,62 +27,54 @@ var randuint32 = require( './rand_uint32.js' );
27
27
// MAIN //
28
28
29
29
/**
30
-
* Generates a pseudorandom integer on the interval \\( [1,2^{31}-1) \\).
30
+
* Generates a pseudorandom integer on the interval \\( [0, 2^{32}) \\) using the PCG32 XSH-RR generator.
31
31
*
32
32
* ## Method
33
33
*
34
-
* Linear congruential generators (LCGs) use the recurrence relation
34
+
* PCG (Permuted Congruential Generator) combines a traditional linear congruential generator (LCG) with an output permutation to improve statistical quality.
35
+
*
36
+
* The internal state is advanced using the LCG recurrence:
35
37
*
36
38
* ```tex
37
-
* X_{n+1} = ( a \cdot X_n + c ) \operatorname{mod}(m)
39
+
* X_{n+1} = a \cdot X_n + c \mod 2^{64}
38
40
* ```
39
41
*
40
-
* where the modulus \\( m \\) is a prime number or power of a prime number and \\( a \\) is a primitive root modulo \\( m \\).
41
-
*
42
-
* <!-- <note> -->
43
-
*
44
-
* For an LCG to be a Lehmer RNG, the seed \\( X_0 \\) must be coprime to \\( m \\).
45
-
*
46
-
* <!-- </note> -->
47
-
*
48
-
* In this implementation, the constants \\( a \\), \\( c \\), and \\( m \\) have the values
42
+
* where:
49
43
*
50
44
* ```tex
51
45
* \begin{align*}
52
-
* a &= 7^5 = 16807 \\
53
-
* c &= 0 \\
54
-
* m &= 2^{31} - 1 = 2147483647
46
+
* a &= 6364136223846793005 \\
47
+
* c &= \text{user-defined increment (must be odd)}
55
48
* \end{align*}
56
49
* ```
57
50
*
51
+
* The output function applies a permutation (XSH-RR variant), which extracts and rotates bits to decorrelate output from internal state:
* The constant \\( m \\) is a Mersenne prime (modulo \\(31\\)).
59
+
* Unlike raw LCGs, PCG’s output permutation helps avoid patterns in the lower bits and produces statistically robust outputs.
61
60
*
62
61
* <!-- </note> -->
63
62
*
64
63
* <!-- <note> -->
65
64
*
66
-
* The constant \\( a \\) is a primitive root (modulo \\(31\\)).
65
+
* The internal LCG uses 64-bit arithmetic, but the output is a 32-bit integer.
67
66
*
68
67
* <!-- </note> -->
69
68
*
70
-
* Accordingly, the maximum possible product is
71
-
*
72
-
* ```tex
73
-
* 16807 \cdot (m - 1) \approx 2^{46}
74
-
* ```
75
-
*
76
-
* The values for \\( a \\), \\( c \\), and \\( m \\) are taken from Park and Miller, "Random Number Generators: Good Ones Are Hard To Find". Park's and Miller's article is also the basis for a recipe in the second edition of _Numerical Recipes in C_.
69
+
* The constants and permutation functions are based on O’Neill's 2014 technical report, _"PCG: A Family of Simple Fast Space-Efficient Statistically Good Algorithms for Random Number Generation"_.
77
70
*
78
71
* ## Notes
79
72
*
80
-
* - The generator has a period of approximately \\(2.1\mbox{e}9\\) (see [Numerical Recipes in C, 2nd Edition](#references), p. 279).
73
+
* - The generator has a period of \\( 2^{64} \\).
81
74
*
82
75
* ## References
83
76
*
84
-
* - Park, S. K., and K. W. Miller. 1988. "Random Number Generators: Good Ones Are Hard to Find." _Communications of the ACM_ 31 (10). New York, NY, USA: ACM: 1192–1201. doi:[10.1145/63039.63042](http://dx.doi.org/10.1145/63039.63042).
85
-
* - Press, William H., Brian P. Flannery, Saul A. Teukolsky, and William T. Vetterling. 1992. _Numerical Recipes in C: The Art of Scientific Computing, Second Edition_. Cambridge University Press.
77
+
* - O’Neill, Melissa E. 2014. “PCG: A Family of Simple Fast Space-Efficient Statistically Good Algorithms for Random Number Generation.” Technical Report HMC-CS-2014-0905. Harvey Mudd College, Claremont, CA. [https://www.cs.hmc.edu/tr/hmc-cs-2014-0905.pdf](https://www.cs.hmc.edu/tr/hmc-cs-2014-0905.pdf).
0 commit comments