Skip to content

Commit 6e2abb9

Browse files
docs: add documentation for pcg32
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent f3dd317 commit 6e2abb9

File tree

11 files changed

+71
-105
lines changed

11 files changed

+71
-105
lines changed

lib/node_modules/@stdlib/random/base/pcg32/README.md

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ limitations under the License.
2020

2121
# PCG32
2222

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.
2424
2525
<section class="usage">
2626

@@ -32,7 +32,7 @@ var pcg32 = require( '@stdlib/random/base/pcg32' );
3232

3333
#### pcg32()
3434

35-
Returns a pseudorandom integer on the interval `[1, 2147483646]`.
35+
Returns a pseudorandom integer on the interval `[0, 4294967295]`.
3636

3737
```javascript
3838
var r = pcg32();
@@ -50,7 +50,7 @@ var r = pcg32.normalized();
5050

5151
#### pcg32.factory( \[options] )
5252

53-
Returns a linear congruential pseudorandom number generator ([LCG][lcg]).
53+
Returns a permuted congruential pseudorandom number generator ([PCG][pcg]).
5454

5555
```javascript
5656
var rand = pcg32.factory();
@@ -59,10 +59,10 @@ var rand = pcg32.factory();
5959
The function accepts the following `options`:
6060

6161
- **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.
6363
- **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`.
6464

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]`
6666

6767
```javascript
6868
var rand = pcg32.factory({
@@ -73,13 +73,13 @@ var r = rand();
7373
// returns 2557507945
7474
```
7575

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
7777

7878
```javascript
79-
var Int32Array = require( '@stdlib/array/int32' );
79+
var Uint32Array = require( '@stdlib/array/uint32' );
8080

8181
var rand = pcg32.factory({
82-
'seed': new Int32Array( [ 1234 ] )
82+
'seed': new Uint32Array( [ 1234 ] )
8383
});
8484

8585
var r = rand();
@@ -234,8 +234,8 @@ var o = pcg32.toJSON();
234234

235235
## Notes
236236

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 fast and 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.
239239
- 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.
240240
- 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).
241241

@@ -286,8 +286,7 @@ for ( i = 0; i < 100; i++ ) {
286286

287287
## References
288288

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].
291290

292291
</section>
293292

@@ -297,17 +296,6 @@ for ( i = 0; i < 100; i++ ) {
297296

298297
<section class="related">
299298

300-
* * *
301-
302-
## See Also
303-
304-
- <span class="package-name">[`@stdlib/random/array/pcg32`][@stdlib/random/array/pcg32]</span><span class="delimiter">: </span><span class="description">create an array containing pseudorandom numbers generated using a linear congruential pseudorandom number generator (LCG).</span>
305-
- <span class="package-name">[`@stdlib/random/iter/pcg32`][@stdlib/random/iter/pcg32]</span><span class="delimiter">: </span><span class="description">create an iterator for a linear congruential pseudorandom number generator (LCG) based on Park and Miller.</span>
306-
- <span class="package-name">[`@stdlib/random/streams/pcg32`][@stdlib/random/streams/pcg32]</span><span class="delimiter">: </span><span class="description">create a readable stream for a linear congruential pseudorandom number generator (LCG) based on Park and Miller.</span>
307-
- <span class="package-name">[`@stdlib/random/base/pcg32-shuffle`][@stdlib/random/base/pcg32-shuffle]</span><span class="delimiter">: </span><span class="description">A linear congruential pseudorandom number generator (LCG) whose output is shuffled.</span>
308-
- <span class="package-name">[`@stdlib/random/base/mt19937`][@stdlib/random/base/mt19937]</span><span class="delimiter">: </span><span class="description">A 32-bit Mersenne Twister pseudorandom number generator.</span>
309-
- <span class="package-name">[`@stdlib/random/base/randi`][@stdlib/random/base/randi]</span><span class="delimiter">: </span><span class="description">pseudorandom numbers having integer values.</span>
310-
311299
</section>
312300

313301
<!-- /.related -->
@@ -316,28 +304,16 @@ for ( i = 0; i < 100; i++ ) {
316304

317305
<section class="links">
318306

319-
[lcg]: https://en.wikipedia.org/wiki/Linear_congruential_generator
307+
[pcg]: https://en.wikipedia.org/wiki/Permuted_congruential_generator
320308

321-
[pros-cons]: https://en.wikipedia.org/wiki/Linear_congruential_generator#Advantages_and_disadvantages_of_LCGs
309+
[lcg]: https://en.wikipedia.org/wiki/Linear_congruential_generator
322310

323-
[@park:1988]: http://dx.doi.org/10.1145/63039.63042
311+
[@oneill:2014]: https://www.cs.hmc.edu/tr/hmc-cs-2014-0905.pdf
324312

325-
[@stdlib/array/int32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/int32
313+
[@stdlib/array/uint32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/uint32
326314

327315
<!-- <related-links> -->
328316

329-
[@stdlib/random/array/pcg32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/array/pcg32
330-
331-
[@stdlib/random/iter/pcg32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/iter/pcg32
332-
333-
[@stdlib/random/streams/pcg32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/streams/pcg32
334-
335-
[@stdlib/random/base/pcg32-shuffle]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/base/pcg32-shuffle
336-
337-
[@stdlib/random/base/mt19937]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/base/mt19937
338-
339-
[@stdlib/random/base/randi]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/base/randi
340-
341317
<!-- </related-links> -->
342318

343319
</section>

lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.factory.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
var bench = require( '@stdlib/bench' );
2424
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25-
var Int32Array = require( '@stdlib/array/int32' );
25+
var Uint32Array = require( '@stdlib/array/uint32' );
2626
var pkg = require( './../package.json' ).name;
2727
var factory = require( './../lib' ).factory;
2828

@@ -81,7 +81,7 @@ bench( pkg+':factory:seed=<array>', function benchmark( b ) {
8181

8282
opts = {};
8383

84-
seed = new Int32Array( 10 );
84+
seed = new Uint32Array( 10 );
8585
for ( i = 0; i < seed.length; i++ ) {
8686
seed[ i ] = 123 + i;
8787
}

lib/node_modules/@stdlib/random/base/pcg32/benchmark/c/benchmark.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ static double benchmark3( void ) {
201201
static double benchmark4( void ) {
202202
double elapsed;
203203
int8_t status;
204-
int32_t v;
204+
uint32_t v;
205205
double t;
206206
int i;
207207

lib/node_modules/@stdlib/random/base/pcg32/docs/repl.txt

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11

22
{{alias}}()
3-
Returns a pseudorandom integer on the interval `[1, 2147483646]`.
3+
Returns a pseudorandom integer on the interval `[0, 4294967295]`.
44

5-
This pseudorandom number generator (PRNG) is a linear congruential
6-
pseudorandom number generator (LCG) based on Park and Miller.
5+
This pseudorandom number generator (PRNG) is a permuted congruential
6+
pseudorandom number generator (PCG) based on O’Neill.
77

8-
The generator has a period of approximately `2.1e9`.
8+
The generator has a period of `2^64`.
99

10-
An LCG is fast and uses little memory. On the other hand, because the
11-
generator is a simple LCG, the generator has recognized shortcomings. By
12-
today's PRNG standards, the generator's period is relatively short. More
13-
importantly, the "randomness quality" of the generator's output is lacking.
14-
These defects make the generator unsuitable, for example, in Monte Carlo
15-
simulations and in cryptographic applications.
10+
A PCG is fast, compact, and has excellent statistical quality compared to
11+
classic LCGs. It uses a simple LCG under the hood but applies permutation
12+
steps to improve output randomness. While it's not cryptographically secure,
13+
it's well-suited for most non-crypto tasks like simulations or procedural
14+
generation.
1615

1716
Returns
1817
-------
@@ -38,7 +37,7 @@
3837

3938

4039
{{alias}}.factory( [options] )
41-
Returns a linear congruential pseudorandom number generator (LCG).
40+
Returns a permuted congruential pseudorandom number generator (PCG).
4241

4342
Parameters
4443
----------
@@ -47,11 +46,11 @@
4746

4847
options.seed: integer|ArrayLikeObject<integer> (optional)
4948
Pseudorandom number generator seed. The seed may be either a positive
50-
signed 32-bit integer on the interval `[1, 2147483646]` or, for
51-
arbitrary length seeds, an array-like object containing signed 32-bit
49+
unsigned 32-bit integer on the interval `[0, 4294967295]` or, for
50+
arbitrary length seeds, an array-like object containing unsigned 32-bit
5251
integers.
5352

54-
options.state: Int32Array (optional)
53+
options.state: Uint32Array (optional)
5554
Pseudorandom number generator state. If provided, the `seed` option is
5655
ignored.
5756

lib/node_modules/@stdlib/random/base/pcg32/docs/types/index.d.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,28 +95,28 @@ interface PRNG {
9595
}
9696

9797
/**
98-
* Interface for generating pseudorandom integers on the interval `[1, 2147483646]`.
98+
* Interface for generating pseudorandom integers on the interval `[0, 4294967295]`.
9999
*/
100100
interface NullaryFunction extends PRNG {
101101
/**
102-
* Returns a pseudorandom integer on the interval `[1, 2147483646]`.
102+
* Returns a pseudorandom integer on the interval `[0, 4294967295]`.
103103
*
104104
* @returns pseudorandom number
105105
*/
106106
(): number;
107107
}
108108

109109
/**
110-
* Interface for generating pseudorandom integers on the interval `[1, 2147483646]`.
110+
* Interface for generating pseudorandom integers on the interval `[0, 4294967295]`.
111111
*/
112112
interface Random extends PRNG {
113113
/**
114-
* Returns a pseudorandom integer on the interval `[1, 2147483646]`.
114+
* Returns a pseudorandom integer on the interval `[0, 4294967295]`.
115115
*
116116
* ## Notes
117117
*
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`.
120120
* - 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.
121121
*
122122
* @returns pseudorandom number
@@ -139,7 +139,7 @@ interface Random extends PRNG {
139139
normalized(): number;
140140

141141
/**
142-
* Returns a linear congruential pseudorandom number generator (LCG).
142+
* Returns a permuted congruential pseudorandom number generator (PCG).
143143
*
144144
* @param options - function options
145145
* @param options.seed - pseudorandom number generator seed
@@ -164,12 +164,12 @@ interface Random extends PRNG {
164164
}
165165

166166
/**
167-
* Returns a pseudorandom integer on the interval `[1, 2147483646]`.
167+
* Returns a pseudorandom integer on the interval `[0, 4294967295]`.
168168
*
169169
* ## Notes
170170
*
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`.
173173
* - 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.
174174
*
175175
* @returns pseudorandom number

lib/node_modules/@stdlib/random/base/pcg32/lib/factory.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ function verifyState( state, FLG ) {
115115
// MAIN //
116116

117117
/**
118-
* Returns a linear congruential pseudorandom number generator (LCG) based on Park and Miller.
118+
* Returns a permuted congruential pseudorandom number generator (PCG) based on O’Neill.
119119
*
120120
* @param {Options} [options] - options
121121
* @param {PRNGSeedPCG32} [options.seed] - pseudorandom number generator seed
@@ -406,7 +406,7 @@ function factory( options ) {
406406
}
407407

408408
/**
409-
* Generates a pseudorandom integer on the interval \\( [1,2^{32}) \\).
409+
* Generates a pseudorandom integer on the interval \\( [0,2^{32}) \\).
410410
*
411411
* @private
412412
* @returns {uinteger32} pseudorandom integer

lib/node_modules/@stdlib/random/base/pcg32/lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
'use strict';
2020

2121
/**
22-
* A linear congruential pseudorandom number generator (LCG) based on Park and Miller.
22+
* A permuted congruential pseudorandom number generator (PCG) based on O’Neill.
2323
*
2424
* @module @stdlib/random/base/pcg32
2525
*

lib/node_modules/@stdlib/random/base/pcg32/lib/main.js

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,62 +27,54 @@ var randuint32 = require( './rand_uint32.js' );
2727
// MAIN //
2828

2929
/**
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.
3131
*
3232
* ## Method
3333
*
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:
3537
*
3638
* ```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}
3840
* ```
3941
*
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:
4943
*
5044
* ```tex
5145
* \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)}
5548
* \end{align*}
5649
* ```
5750
*
51+
* The output function applies a permutation (XSH-RR variant), which extracts and rotates bits to decorrelate output from internal state:
52+
*
53+
* ```tex
54+
* \text{output} = \operatorname{rotr}\left((x \gg ((x \gg 59) + 5)) \bmod 2^{32}, x \gg 27\right)
55+
* ```
56+
*
5857
* <!-- <note> -->
5958
*
60-
* 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.
6160
*
6261
* <!-- </note> -->
6362
*
6463
* <!-- <note> -->
6564
*
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.
6766
*
6867
* <!-- </note> -->
6968
*
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"_.
7770
*
7871
* ## Notes
7972
*
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} \\).
8174
*
8275
* ## References
8376
*
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).
8678
*
8779
* @function pcg32
8880
* @type {PRNG}

0 commit comments

Comments
 (0)