Gathering detailed insights and metrics for @stdlib/random-base-mt19937
Gathering detailed insights and metrics for @stdlib/random-base-mt19937
Gathering detailed insights and metrics for @stdlib/random-base-mt19937
Gathering detailed insights and metrics for @stdlib/random-base-mt19937
A 32-bit Mersenne Twister pseudorandom number generator.
npm install @stdlib/random-base-mt19937
Typescript
Module System
Min. Node Version
Node Version
NPM Version
86.9
Supply Chain
81.8
Quality
80.7
Maintenance
100
Vulnerability
87.6
License
JavaScript (54.26%)
C (45.74%)
Total Downloads
6,539,825
Last Day
2,161
Last Week
12,442
Last Month
59,585
Last Year
744,801
Apache-2.0 License
7 Stars
58 Commits
3 Watchers
5 Branches
12 Contributors
Updated on Apr 01, 2025
Minified
Minified + Gzipped
Latest Version
0.2.1
Package Id
@stdlib/random-base-mt19937@0.2.1
Unpacked Size
143.39 kB
Size
32.89 kB
File Count
16
NPM Version
8.19.4
Node Version
16.20.2
Published on
Feb 25, 2024
Cumulative downloads
Total Downloads
Last Day
16.7%
2,161
Compared to previous day
Last Week
-6%
12,442
Compared to previous week
Last Month
-27.1%
59,585
Compared to previous month
Last Year
-84.7%
744,801
Compared to previous year
20
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!
A 32-bit Mersenne Twister pseudorandom number generator.
1npm install @stdlib/random-base-mt19937
1var mt19937 = require( '@stdlib/random-base-mt19937' );
Returns a pseudorandom integer on the interval [0, 4294967295]
.
1var r = mt19937(); 2// returns <number>
Returns a pseudorandom number on the interval [0, 1)
with 53-bit precision.
1var r = mt19937.normalized(); 2// returns <number>
Returns a 32-bit Mersenne Twister pseudorandom number generator.
1var rand = mt19937.factory();
The function accepts the following options
:
Uint32Array
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. Setting this option to true
ensures that a returned generator has exclusive control over its internal state. Default: true
.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]
1var rand = mt19937.factory({ 2 'seed': 1234 3}); 4 5var r = rand(); 6// returns 822569775
or, for arbitrary length seeds, an array-like object
containing unsigned 32-bit integers
1var Uint32Array = require( '@stdlib/array-uint32' ); 2 3var rand = mt19937.factory({ 4 'seed': new Uint32Array( [ 291, 564, 837, 1110 ] ) 5}); 6 7var r = rand(); 8// returns 1067595299
To return a generator having a specific initial state, set the generator state
option.
1var rand; 2var bool; 3var r; 4var i; 5 6// Generate pseudorandom numbers, thus progressing the generator state: 7for ( i = 0; i < 1000; i++ ) { 8 r = mt19937(); 9} 10 11// Create a new MT19937 PRNG initialized to the current state of `mt19937`: 12rand = mt19937.factory({ 13 'state': mt19937.state 14}); 15 16// Test that the generated pseudorandom numbers are the same: 17bool = ( rand() === mt19937() ); 18// returns true
The generator name.
1var str = mt19937.NAME; 2// returns 'mt19937'
Minimum possible value.
1var min = mt19937.MIN; 2// returns 0
Maximum possible value.
1var max = mt19937.MAX; 2// returns 4294967295
The value used to seed mt19937()
.
1var rand; 2var r; 3var i; 4 5// Generate pseudorandom values... 6for ( i = 0; i < 100; i++ ) { 7 r = mt19937(); 8} 9 10// Generate the same pseudorandom values... 11rand = mt19937.factory({ 12 'seed': mt19937.seed 13}); 14for ( i = 0; i < 100; i++ ) { 15 r = rand(); 16}
Length of generator seed.
1var len = mt19937.seedLength; 2// returns <number>
Writable property for getting and setting the generator state.
1var r = mt19937(); 2// returns <number> 3 4r = mt19937(); 5// returns <number> 6 7// ... 8 9// Get a copy of the current state: 10var state = mt19937.state; 11// returns <Uint32Array> 12 13r = mt19937(); 14// returns <number> 15 16r = mt19937(); 17// returns <number> 18 19// Reset the state: 20mt19937.state = state; 21 22// Replay the last two pseudorandom numbers: 23r = mt19937(); 24// returns <number> 25 26r = mt19937(); 27// returns <number> 28 29// ...
Length of generator state.
1var len = mt19937.stateLength; 2// returns <number>
Size (in bytes) of generator state.
1var sz = mt19937.byteLength; 2// returns <number>
Serializes the pseudorandom number generator as a JSON object.
1var o = mt19937.toJSON(); 2// returns { 'type': 'PRNG', 'name': '...', 'state': {...}, 'params': [] }
~2.5kB
). Because of the large state size, beware of increased memory consumption when using the factory()
method to create many Mersenne Twister PRNGs. When appropriate (e.g., when external state mutation is not a concern), consider sharing PRNG state.1
is considered equivalent to an integer seed equal to the lone seed array element and vice versa.2^19937 - 1
.1var mt19937 = require( '@stdlib/random-base-mt19937' ); 2 3var seed; 4var rand; 5var i; 6 7// Generate pseudorandom numbers... 8for ( i = 0; i < 100; i++ ) { 9 console.log( mt19937() ); 10} 11 12// Create a new pseudorandom number generator... 13seed = 1234; 14rand = mt19937.factory({ 15 'seed': seed 16}); 17for ( i = 0; i < 100; i++ ) { 18 console.log( rand() ); 19} 20 21// Create another pseudorandom number generator using a previous seed... 22rand = mt19937.factory({ 23 'seed': mt19937.seed 24}); 25for ( i = 0; i < 100; i++ ) { 26 console.log( rand() ); 27}
@stdlib/random-array/mt19937
: create an array containing pseudorandom numbers generated using a 32-bit Mersenne Twister pseudorandom number generator.@stdlib/random-iter/mt19937
: create an iterator for a 32-bit Mersenne Twister pseudorandom number generator.@stdlib/random-streams/mt19937
: create a readable stream for a 32-bit Mersenne Twister pseudorandom number generator.@stdlib/random-base/minstd
: A linear congruential pseudorandom number generator (LCG) based on Park and Miller.@stdlib/random-base/randi
: pseudorandom numbers having integer values.This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
Copyright © 2016-2024. The Stdlib Authors.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
4 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 3
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
no SAST tool detected
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Score
Last Scanned on 2025-04-28
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More