Skip to content

Commit 541f80a

Browse files
committed
Add PRNG for generating numbers having integer values
1 parent b69b06d commit 541f80a

17 files changed

Lines changed: 1219 additions & 0 deletions

File tree

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
# randi
2+
3+
> Pseudorandom numbers having integer values.
4+
5+
<section class="usage">
6+
7+
## Usage
8+
9+
```javascript
10+
var randi = require( '@stdlib/math/base/random/randi' );
11+
```
12+
13+
#### randi()
14+
15+
Returns a pseudorandom number having an integer value.
16+
17+
```javascript
18+
var v = randi();
19+
// returns <number>
20+
```
21+
22+
#### randi.factory( \[options] )
23+
24+
Returns a pseudorandom number generator (PRNG) for generating random numbers.
25+
26+
```javascript
27+
var rand = randi.factory();
28+
```
29+
30+
The function accepts the following `options`:
31+
32+
- **name**: name of a supported pseudorandom number generator (PRNG), which will serve as the underlying source of pseudorandom numbers. Default: `'minstd-shuffle'`.
33+
- **seed**: pseudorandom number generator seed.
34+
35+
By default, the underlying pseudorandom number generator is [`minstd-shuffle`][@stdlib/math/base/random/minstd-shuffle]. To use a different PRNG, set the `name` option.
36+
37+
```javascript
38+
var rand = randi.factory({
39+
'name': 'minstd'
40+
});
41+
42+
var v = rand();
43+
// returns <number>
44+
```
45+
46+
To seed a pseudorandom number generator, set the `seed` option.
47+
48+
```javascript
49+
var rand = randi.factory({
50+
'seed': 12345
51+
});
52+
53+
var v = rand();
54+
// returns <number>
55+
```
56+
57+
#### randi.NAME
58+
59+
The generator name.
60+
61+
```javascript
62+
var str = randi.NAME;
63+
// returns 'randi'
64+
```
65+
66+
#### randi.PRNG
67+
68+
The underlying pseudorandom number generator.
69+
70+
```javascript
71+
var prng = randi.PRNG;
72+
// returns <Function>
73+
```
74+
75+
#### randi.SEED
76+
77+
The value used to seed `randi()`.
78+
79+
```javascript
80+
var rand;
81+
var v;
82+
var i;
83+
84+
// Generate pseudorandom values...
85+
for ( i = 0; i < 100; i++ ) {
86+
v = randi();
87+
}
88+
89+
// Generate the same pseudorandom values...
90+
rand = randi.factory({
91+
'seed': randi.SEED
92+
});
93+
for ( i = 0; i < 100; i++ ) {
94+
v = rand();
95+
}
96+
```
97+
98+
#### randi.MIN
99+
100+
Minimum value lower bound (specific to underlying PRNG).
101+
102+
```javascript
103+
var min = randi.MIN;
104+
// returns <number>
105+
```
106+
107+
#### randi.MAX
108+
109+
Maximum value upper bound (specific to underlying PRNG).
110+
111+
```javascript
112+
var max = randi.MAX;
113+
// returns <number>
114+
```
115+
116+
</section>
117+
118+
<!-- /.usage -->
119+
120+
<section class="notes">
121+
122+
## Notes
123+
124+
- **Warning**: the default underlying source of pseudorandom numbers may **change** in the future. If exact reproducibility is required, either explicitly specify a PRNG via the `name` option or use an underlying PRNG directly.
125+
126+
</section>
127+
128+
<!-- /.notes -->
129+
130+
<section class="examples">
131+
132+
## Examples
133+
134+
```javascript
135+
var randi = require( '@stdlib/math/base/random/randi' );
136+
137+
var seed;
138+
var rand;
139+
var i;
140+
141+
// Generate pseudorandom numbers...
142+
for ( i = 0; i < 100; i++ ) {
143+
console.log( randi() );
144+
}
145+
146+
// Create a new pseudorandom number generator...
147+
seed = 1234;
148+
rand = randi.factory({
149+
'seed': seed
150+
});
151+
for ( i = 0; i < 100; i++ ) {
152+
console.log( rand() );
153+
}
154+
155+
// Create another pseudorandom number generator using a previous seed...
156+
rand = randi.factory({
157+
'seed': randi.SEED
158+
});
159+
for ( i = 0; i < 100; i++ ) {
160+
console.log( rand() );
161+
}
162+
```
163+
164+
</section>
165+
166+
<!-- /.examples -->
167+
168+
<section class="links">
169+
170+
[@stdlib/math/base/random/minstd-shuffle]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/random/minstd-shuffle
171+
172+
</section>
173+
174+
<!-- /.links -->
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
3+
// MODULES //
4+
5+
var bench = require( '@stdlib/bench' );
6+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
7+
var floor = require( '@stdlib/math/base/special/floor' );
8+
var UINT32_MAX = require( '@stdlib/math/constants/uint32-max' );
9+
var pkg = require( './../package.json' ).name;
10+
var randi = require( './../lib' );
11+
12+
13+
// MAIN //
14+
15+
bench( pkg, function benchmark( b ) {
16+
var z;
17+
var i;
18+
19+
b.tic();
20+
for ( i = 0; i < b.iterations; i++ ) {
21+
z = randi();
22+
if ( isnan( z ) ) {
23+
b.fail( 'should not return NaN' );
24+
}
25+
}
26+
b.toc();
27+
if ( isnan( z ) ) {
28+
b.fail( 'should not return NaN' );
29+
}
30+
b.pass( 'benchmark finished' );
31+
b.end();
32+
});
33+
34+
bench( pkg+'::built-in', function benchmark( b ) {
35+
var z;
36+
var i;
37+
38+
b.tic();
39+
for ( i = 0; i < b.iterations; i++ ) {
40+
z = floor( Math.random()*UINT32_MAX ); // not robust
41+
if ( isnan( z ) ) {
42+
b.fail( 'should not return NaN' );
43+
}
44+
}
45+
b.toc();
46+
if ( isnan( z ) ) {
47+
b.fail( 'should not return NaN' );
48+
}
49+
b.pass( 'benchmark finished' );
50+
b.end();
51+
});
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
2+
# VARIABLES #
3+
4+
ifndef VERBOSE
5+
QUIET := @
6+
endif
7+
8+
# Determine the OS:
9+
#
10+
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
11+
# [2]: http://stackoverflow.com/a/27776822/2225624
12+
OS ?= $(shell uname)
13+
ifneq (, $(findstring MINGW,$(OS)))
14+
OS := WINNT
15+
else
16+
ifneq (, $(findstring MSYS,$(OS)))
17+
OS := WINNT
18+
else
19+
ifneq (, $(findstring CYGWIN,$(OS)))
20+
OS := WINNT
21+
endif
22+
endif
23+
endif
24+
25+
# Define the program used for compiling C source files:
26+
ifdef C_COMPILER
27+
CC := $(C_COMPILER)
28+
else
29+
CC := gcc
30+
endif
31+
32+
# Define the command-line options when compiling C files:
33+
CFLAGS ?= \
34+
-std=c99 \
35+
-O3 \
36+
-Wall \
37+
-pedantic
38+
39+
# Determine whether to generate [position independent code][1]:
40+
#
41+
# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options
42+
# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option
43+
ifeq ($(OS), WINNT)
44+
fPIC ?=
45+
else
46+
fPIC ?= -fPIC
47+
endif
48+
49+
# List of C targets:
50+
c_targets := benchmark.out
51+
52+
53+
# TARGETS #
54+
55+
# Default target.
56+
#
57+
# This target is the default target.
58+
59+
all: $(c_targets)
60+
61+
.PHONY: all
62+
63+
64+
# Compile C source.
65+
#
66+
# This target compiles C source files.
67+
68+
$(c_targets): %.out: %.c
69+
$(QUIET) $(CC) $(CFLAGS) $(fPIC) -o $@ $<
70+
71+
72+
# Run a benchmark.
73+
#
74+
# This target runs a benchmark.
75+
76+
run: $(c_targets)
77+
$(QUIET) ./$<
78+
79+
.PHONY: run
80+
81+
82+
# Perform clean-up.
83+
#
84+
# This target removes generated files.
85+
86+
clean:
87+
$(QUIET) -rm -f *.o *.out
88+
89+
.PHONY: clean

0 commit comments

Comments
 (0)