Skip to content

Commit f38e07c

Browse files
chore: initial commit
--- 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: passed - 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: passed - task: lint_c_examples status: passed - 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: passed - task: lint_license_headers status: passed ---
1 parent 13b7262 commit f38e07c

File tree

22 files changed

+4959
-0
lines changed

22 files changed

+4959
-0
lines changed
Lines changed: 345 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,345 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2018 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# MINSTD
22+
23+
> A linear congruential pseudorandom number generator ([LCG][lcg]) based on Park and Miller.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var minstd = require( '@stdlib/random/base/minstd' );
31+
```
32+
33+
#### minstd()
34+
35+
Returns a pseudorandom integer on the interval `[1, 2147483646]`.
36+
37+
```javascript
38+
var r = minstd();
39+
// returns <number>
40+
```
41+
42+
#### minstd.normalized()
43+
44+
Returns a pseudorandom number on the interval `[0,1)`.
45+
46+
```javascript
47+
var r = minstd.normalized();
48+
// returns <number>
49+
```
50+
51+
#### minstd.factory( \[options] )
52+
53+
Returns a linear congruential pseudorandom number generator ([LCG][lcg]).
54+
55+
```javascript
56+
var rand = minstd.factory();
57+
```
58+
59+
The function accepts the following `options`:
60+
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.
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+
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]`
66+
67+
```javascript
68+
var rand = minstd.factory({
69+
'seed': 1234
70+
});
71+
72+
var r = rand();
73+
// returns 20739838
74+
```
75+
76+
or, for arbitrary length seeds, an array-like `object` containing signed 32-bit integers
77+
78+
```javascript
79+
var Int32Array = require( '@stdlib/array/int32' );
80+
81+
var rand = minstd.factory({
82+
'seed': new Int32Array( [ 1234 ] )
83+
});
84+
85+
var r = rand();
86+
// returns 20739838
87+
```
88+
89+
To return a generator having a specific initial state, set the generator `state` option.
90+
91+
```javascript
92+
// Generate pseudorandom numbers, thus progressing the generator state:
93+
var r;
94+
var i;
95+
for ( i = 0; i < 1000; i++ ) {
96+
r = minstd();
97+
}
98+
99+
// Create a new PRNG initialized to the current state of `minstd`:
100+
var rand = minstd.factory({
101+
'state': minstd.state
102+
});
103+
104+
// Test that the generated pseudorandom numbers are the same:
105+
var bool = ( rand() === minstd() );
106+
// returns true
107+
```
108+
109+
#### minstd.NAME
110+
111+
The generator name.
112+
113+
```javascript
114+
var str = minstd.NAME;
115+
// returns 'minstd'
116+
```
117+
118+
#### minstd.MIN
119+
120+
Minimum possible value.
121+
122+
```javascript
123+
var min = minstd.MIN;
124+
// returns 1
125+
```
126+
127+
#### minstd.MAX
128+
129+
Maximum possible value.
130+
131+
```javascript
132+
var max = minstd.MAX;
133+
// returns 2147483646
134+
```
135+
136+
#### minstd.seed
137+
138+
The value used to seed `minstd()`.
139+
140+
```javascript
141+
// Generate pseudorandom values...
142+
var r;
143+
var i;
144+
for ( i = 0; i < 100; i++ ) {
145+
r = minstd();
146+
}
147+
148+
// Generate the same pseudorandom values...
149+
var rand = minstd.factory({
150+
'seed': minstd.seed
151+
});
152+
for ( i = 0; i < 100; i++ ) {
153+
r = rand();
154+
}
155+
```
156+
157+
#### minstd.seedLength
158+
159+
Length of generator seed.
160+
161+
```javascript
162+
var len = minstd.seedLength;
163+
// returns <number>
164+
```
165+
166+
#### minstd.state
167+
168+
Writable property for getting and setting the generator state.
169+
170+
```javascript
171+
var r = minstd();
172+
// returns <number>
173+
174+
r = minstd();
175+
// returns <number>
176+
177+
// ...
178+
179+
// Get the current state:
180+
var state = minstd.state;
181+
// returns <Int32Array>
182+
183+
r = minstd();
184+
// returns <number>
185+
186+
r = minstd();
187+
// returns <number>
188+
189+
// Reset the state:
190+
minstd.state = state;
191+
192+
// Replay the last two pseudorandom numbers:
193+
r = minstd();
194+
// returns <number>
195+
196+
r = minstd();
197+
// returns <number>
198+
199+
// ...
200+
```
201+
202+
#### minstd.stateLength
203+
204+
Length of generator state.
205+
206+
```javascript
207+
var len = minstd.stateLength;
208+
// returns <number>
209+
```
210+
211+
#### minstd.byteLength
212+
213+
Size (in bytes) of generator state.
214+
215+
```javascript
216+
var sz = minstd.byteLength;
217+
// returns <number>
218+
```
219+
220+
#### minstd.toJSON()
221+
222+
Serializes the pseudorandom number generator as a JSON object.
223+
224+
```javascript
225+
var o = minstd.toJSON();
226+
// returns { 'type': 'PRNG', 'name': '...', 'state': {...}, 'params': [] }
227+
```
228+
229+
</section>
230+
231+
<!-- /.usage -->
232+
233+
<section class="notes">
234+
235+
## Notes
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 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].
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+
- 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+
242+
</section>
243+
244+
<!-- /.notes -->
245+
246+
<section class="examples">
247+
248+
## Examples
249+
250+
<!-- eslint no-undef: "error" -->
251+
252+
```javascript
253+
var minstd = require( '@stdlib/random/base/minstd' );
254+
255+
// Generate pseudorandom numbers...
256+
var i;
257+
for ( i = 0; i < 100; i++ ) {
258+
console.log( minstd() );
259+
}
260+
261+
// Create a new pseudorandom number generator...
262+
var seed = 1234;
263+
var rand = minstd.factory({
264+
'seed': seed
265+
});
266+
for ( i = 0; i < 100; i++ ) {
267+
console.log( rand() );
268+
}
269+
270+
// Create another pseudorandom number generator using a previous seed...
271+
rand = minstd.factory({
272+
'seed': minstd.seed
273+
});
274+
for ( i = 0; i < 100; i++ ) {
275+
console.log( rand() );
276+
}
277+
```
278+
279+
</section>
280+
281+
<!-- /.examples -->
282+
283+
* * *
284+
285+
<section class="references">
286+
287+
## References
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.
291+
292+
</section>
293+
294+
<!-- /.references -->
295+
296+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
297+
298+
<section class="related">
299+
300+
* * *
301+
302+
## See Also
303+
304+
- <span class="package-name">[`@stdlib/random/array/minstd`][@stdlib/random/array/minstd]</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/minstd`][@stdlib/random/iter/minstd]</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/minstd`][@stdlib/random/streams/minstd]</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/minstd-shuffle`][@stdlib/random/base/minstd-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+
311+
</section>
312+
313+
<!-- /.related -->
314+
315+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
316+
317+
<section class="links">
318+
319+
[lcg]: https://en.wikipedia.org/wiki/Linear_congruential_generator
320+
321+
[pros-cons]: https://en.wikipedia.org/wiki/Linear_congruential_generator#Advantages_and_disadvantages_of_LCGs
322+
323+
[@park:1988]: http://dx.doi.org/10.1145/63039.63042
324+
325+
[@stdlib/array/int32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/int32
326+
327+
<!-- <related-links> -->
328+
329+
[@stdlib/random/array/minstd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/array/minstd
330+
331+
[@stdlib/random/iter/minstd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/iter/minstd
332+
333+
[@stdlib/random/streams/minstd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/streams/minstd
334+
335+
[@stdlib/random/base/minstd-shuffle]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/base/minstd-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+
341+
<!-- </related-links> -->
342+
343+
</section>
344+
345+
<!-- /.links -->

0 commit comments

Comments
 (0)