Create a readable stream for generating pseudorandom numbers drawn from a Cauchy distribution.
We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we’ve built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.
The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.
When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.
To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!
[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url]
Create a [readable stream][readable-stream] for generating pseudorandom numbers drawn from a [Cauchy][cauchy] distribution.
bash
npm install @stdlib/random-streams-cauchy
script
tag without installation and bundlers, use the [ES Module][es-module] available on the [esm
][esm-url] branch (see [README][esm-readme]).deno
][deno-url] branch (see [README][deno-readme] for usage intructions).umd
][umd-url] branch (see [README][umd-readme]).javascript
var randomStream = require( '@stdlib/random-streams-cauchy' );
x0
(location) and gamma
(scale).javascript
var inspectStream = require( '@stdlib/streams-node-inspect-sink' );
var iStream;
var stream;
function log( chunk, idx ) {
console.log( chunk.toString() );
if ( idx === 10 ) {
stream.destroy();
}
}
stream = randomStream( -2.0, 2.0 );
iStream = inspectStream( log );
stream.pipe( iStream );
options
:false
.Buffer
objects should be decoded to strings
. Default: null
.'\n'
.[0,1)
. If provided, the function ignores both the state
and seed
options. In order to seed the returned pseudorandom number generator stream, one must seed the provided prng
(assuming the provided prng
is seedable).Uint32Array
][@stdlib/array/uint32] containing pseudorandom number generator state. If provided, the function ignores the seed
option.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 and/or streams. Setting this option to true
ensures that a stream generator has exclusive control over its internal state. Default: true
.1e308
.options
,javascript
var opts = {
'objectMode': true,
'encoding': 'utf8',
'highWaterMark': 64
};
var stream = randomStream( 0.0, 2.0, opts );
iter
option.javascript
var inspectStream = require( '@stdlib/streams-node-inspect-sink' );
function log( chunk ) {
console.log( chunk.toString() );
}
var opts = {
'iter': 10
};
var stream = randomStream( 0.0, 1.0, opts );
var iStream = inspectStream( log );
stream.pipe( iStream );
sep
option.javascript
var inspectStream = require( '@stdlib/streams-node-inspect-sink' );
function log( chunk ) {
console.log( chunk.toString() );
}
var opts = {
'iter': 10,
'sep': ','
};
var stream = randomStream( 2.0, 1.0, opts );
var iStream = inspectStream( log );
stream.pipe( iStream );
seed
option.javascript
var inspectStream = require( '@stdlib/streams-node-inspect-sink' );
function log( v ) {
console.log( v );
}
var opts = {
'objectMode': true,
'iter': 10,
'seed': 1234
};
var stream = randomStream( 2.0, 1.0, opts );
opts = {
'objectMode': true
};
var iStream = inspectStream( opts, log );
stream.pipe( iStream );
state
option.javascript
var inspectStream = require( '@stdlib/streams-node-inspect-sink' );
function log( v ) {
console.log( v );
}
var opts1 = {
'objectMode': true,
'iter': 10
};
var stream = randomStream( 2.0, 2.0, opts1 );
var opts2 = {
'objectMode': true
};
var iStream = inspectStream( opts2, log );
// Stream pseudorandom numbers, thus progressing the underlying generator state:
stream.pipe( iStream );
// Create a new PRNG stream initialized to the last state of the previous stream:
var opts3 = {
'objectMode': true,
'iter': 10,
'state': stream.state
};
stream = randomStream( 2.0, 2.0, opts3 );
iStream = inspectStream( opts2, log );
// Stream pseudorandom numbers starting from the last state of the previous stream:
stream.pipe( iStream );
javascript
var stream = randomStream( 2.0, 2.0 );
var prng = stream.PRNG;
// returns <Function>
javascript
var stream = randomStream( 2.0, 2.0 );
var seed = stream.seed;
// returns <Uint32Array>
null
.javascript
var minstd = require( '@stdlib/random-base-minstd-shuffle' ).normalized;
var stream = randomStream( 2.0, 2.0, {
'prng': minstd
});
var seed = stream.seed;
// returns null
javascript
var stream = randomStream( 2.0, 2.0 );
var len = stream.seedLength;
// returns <number>
null
.javascript
var minstd = require( '@stdlib/random-base-minstd-shuffle' ).normalized;
var stream = randomStream( 2.0, 2.0, {
'prng': minstd
});
var len = stream.seedLength;
// returns null
javascript
var stream = randomStream( 2.0, 2.0 );
var state = stream.state;
// returns <Uint32Array>
null
.javascript
var minstd = require( '@stdlib/random-base-minstd-shuffle' ).normalized;
var stream = randomStream( 2.0, 2.0, {
'prng': minstd
});
var state = stream.state;
// returns null
javascript
var stream = randomStream( 2.0, 2.0 );
var len = stream.stateLength;
// returns <number>
null
.javascript
var minstd = require( '@stdlib/random-base-minstd-shuffle' ).normalized;
var stream = randomStream( 2.0, 2.0, {
'prng': minstd
});
var len = stream.stateLength;
// returns null
javascript
var stream = randomStream( 2.0, 2.0 );
var sz = stream.byteLength;
// returns <number>
null
.javascript
var minstd = require( '@stdlib/random-base-minstd-shuffle' ).normalized;
var stream = randomStream( 2.0, 2.0, {
'prng': minstd
});
var sz = stream.byteLength;
// returns null
function
for creating [readable streams][readable-stream] which generate pseudorandom numbers drawn from a [Cauchy][cauchy] distribution.javascript
var opts = {
'objectMode': true,
'encoding': 'utf8',
'highWaterMark': 64
};
var createStream = randomStream.factory( opts );
javascript
var createStream = randomStream.factory( 2.0, 2.0 );
var stream1 = createStream();
var stream2 = createStream();
// ...
javascript
var createStream = randomStream.factory();
var stream1 = createStream( 2.0, 2.0 );
var stream2 = createStream( 2.0, 2.0 );
// ...
options
as randomStream()
.
javascript
var inspectStream = require( '@stdlib/streams-node-inspect-sink' );
function log( v ) {
console.log( v );
}
var opts = {
'iter': 10
};
var stream = randomStream.objectMode( 2.0, 2.0, opts );
opts = {
'objectMode': true
};
var iStream = inspectStream( opts, log );
stream.pipe( iStream );
options
as randomStream()
; however, the method will always override the [objectMode
][object-mode] option in options
.siter
pseudorandom numbers.javascript
var opts = {
'siter': 10 // emit the PRNG state every 10 pseudorandom numbers
};
var stream = randomStream( 2.0, 2.0, opts );
stream.on( 'state', onState );
function onState( state ) {
// Do something with the emitted state, such as save to file...
}
siter
option in conjunction with a state
event listener. Attempting to capture the underlying PRNG state after reading generated numbers is not likely to give expected results, as internal stream buffering will mean more values have been generated than have been read. Thus, the state returned by the state
property will likely reflect a future PRNG state from the perspective of downstream consumers.javascript
var inspectStream = require( '@stdlib/streams-node-inspect-sink' );
var randomStream = require( '@stdlib/random-streams-cauchy' );
function log( v ) {
console.log( v.toString() );
}
var opts = {
'objectMode': true,
'iter': 10
};
var stream = randomStream( 2.0, 2.0, opts );
opts = {
'objectMode': true
};
var iStream = inspectStream( opts, log );
stream.pipe( iStream );
bash
npm install -g @stdlib/random-streams-cauchy-cli
text
Usage: random-cauchy [options] <x0> <gamma>
Options:
-h, --help Print this message.
-V, --version Print the package version.
--sep sep Separator used to join streamed data. Default: '\n'.
-n, --iter iterations Number of pseudorandom numbers.
--seed seed Pseudorandom number generator seed.
--state filepath Path to a file containing the pseudorandom number
generator state.
--snapshot filepath Output file path for saving the pseudorandom number
generator state upon exit.
bash
$ random-cauchy 0.0 1.0 -n 10 --seed 1234