Skip to content

Commit 101fcbc

Browse files
committed
Add Bernoulli PRNG
1 parent e859269 commit 101fcbc

File tree

12 files changed

+1274
-0
lines changed

12 files changed

+1274
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
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+
# Bernoulli Random Numbers
22+
23+
> [Bernoulli][bernoulli] distributed pseudorandom numbers.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
31+
```
32+
33+
#### bernoulli( p )
34+
35+
Returns a pseudorandom number drawn from a [Bernoulli][bernoulli] distribution with success probability `p`.
36+
37+
```javascript
38+
var r = bernoulli( 0.8 );
39+
// returns <number>
40+
```
41+
42+
If `p < 0` or `p > 1`, the function returns `NaN`.
43+
44+
```javascript
45+
var r = bernoulli( 3.14 );
46+
// returns NaN
47+
48+
r = bernoulli( -0.5 );
49+
// returns NaN
50+
```
51+
52+
If `p` is `NaN`, the function returns `NaN`.
53+
54+
```javascript
55+
var r = bernoulli( NaN );
56+
// returns NaN
57+
```
58+
59+
#### bernoulli.factory( \[p, ]\[options] )
60+
61+
Returns a pseudorandom number generator (PRNG) for generating pseudorandom numbers drawn from a [Bernoulli][bernoulli] distribution.
62+
63+
```javascript
64+
var rand = bernoulli.factory();
65+
66+
var r = rand( 0.3 );
67+
// returns <number>
68+
```
69+
70+
If provided `p`, the returned generator returns random variates from the specified distribution.
71+
72+
```javascript
73+
var rand = bernoulli.factory( 0.6 );
74+
75+
var r = rand();
76+
// returns <number>
77+
78+
r = rand();
79+
// returns <number>
80+
```
81+
82+
If not provided `p`, the returned generator requires that `p` be provided at each invocation.
83+
84+
```javascript
85+
var rand = bernoulli.factory();
86+
87+
var r = rand( 0.67 );
88+
// returns <number>
89+
90+
r = rand( 0.42 );
91+
// returns <number>
92+
```
93+
94+
The function accepts the following `options`:
95+
96+
- **seed**: pseudorandom number generator seed.
97+
98+
To seed a pseudorandom number generator, set the `seed` option.
99+
100+
```javascript
101+
var rand = bernoulli.factory({
102+
'seed': 12345
103+
});
104+
105+
var r = bernoulli( 0.9 );
106+
// returns <number>
107+
108+
rand = bernoulli.factory( 0.2, {
109+
'seed': 12345
110+
});
111+
112+
r = rand();
113+
// returns <number>
114+
```
115+
116+
#### bernoulli.NAME
117+
118+
The generator name.
119+
120+
```javascript
121+
var str = bernoulli.NAME;
122+
// returns 'bernoulli'
123+
```
124+
125+
#### bernoulli.PRNG
126+
127+
The underlying pseudorandom number generator.
128+
129+
```javascript
130+
var prng = bernoulli.PRNG;
131+
// returns <Function>
132+
```
133+
134+
#### bernoulli.SEED
135+
136+
The value used to seed `bernoulli()`.
137+
138+
```javascript
139+
var rand;
140+
var r;
141+
var i;
142+
143+
// Generate pseudorandom values...
144+
for ( i = 0; i < 100; i++ ) {
145+
r = bernoulli( 20, 0.5 );
146+
}
147+
148+
// Generate the same pseudorandom values...
149+
rand = bernoulli.factory( 20, 0.5, {
150+
'seed': bernoulli.SEED
151+
});
152+
for ( i = 0; i < 100; i++ ) {
153+
r = rand();
154+
}
155+
```
156+
157+
</section>
158+
159+
<!-- /.usage -->
160+
161+
<section class="examples">
162+
163+
## Examples
164+
165+
<!-- eslint no-undef: "error" -->
166+
167+
```javascript
168+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
169+
170+
var seed;
171+
var rand;
172+
var i;
173+
174+
// Generate pseudorandom numbers...
175+
console.log( '\nseed: %d', bernoulli.SEED );
176+
for ( i = 0; i < 100; i++ ) {
177+
console.log( bernoulli( 0.4 ) );
178+
}
179+
180+
// Create a new pseudorandom number generator...
181+
seed = 1234;
182+
rand = bernoulli.factory( 0.4, {
183+
'seed': seed
184+
});
185+
console.log( '\nseed: %d', seed );
186+
for ( i = 0; i < 100; i++ ) {
187+
console.log( rand() );
188+
}
189+
190+
// Create another pseudorandom number generator using a previous seed...
191+
rand = bernoulli.factory( 0.4, {
192+
'seed': bernoulli.SEED
193+
});
194+
console.log( '\nseed: %d', bernoulli.SEED );
195+
for ( i = 0; i < 100; i++ ) {
196+
console.log( rand() );
197+
}
198+
```
199+
200+
</section>
201+
202+
<!-- /.examples -->
203+
204+
<section class="links">
205+
206+
[bernoulli]: https://en.wikipedia.org/wiki/Bernoulli_distribution
207+
208+
</section>
209+
210+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var randu = require( '@stdlib/random/base/randu' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pkg = require( './../package.json' ).name;
27+
var bernoulli = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var p;
34+
var z;
35+
var i;
36+
37+
b.tic();
38+
for ( i = 0; i < b.iterations; i++ ) {
39+
p = randu();
40+
z = bernoulli( p );
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+
});
52+
53+
bench( pkg+':factory', function benchmark( b ) {
54+
var rand;
55+
var p;
56+
var z;
57+
var i;
58+
59+
p = 0.7549;
60+
rand = bernoulli.factory( p );
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
z = rand();
65+
if ( isnan( z ) ) {
66+
b.fail( 'should not return NaN' );
67+
}
68+
}
69+
b.toc();
70+
if ( isnan( z ) ) {
71+
b.fail( 'should not return NaN' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Package: rbinom-benchmarks
2+
Title: Benchmarks
3+
Version: 0.0.0
4+
Authors@R: person("stdlib", "js", role = c("aut","cre"))
5+
Description: Benchmarks.
6+
Depends: R (>=3.3.3)
7+
Imports:
8+
microbenchmark
9+
LazyData: true

0 commit comments

Comments
 (0)