Research
Security News
Kill Switch Hidden in npm Packages Typosquatting Chalk and Chokidar
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
mersenne-twister
Advanced tools
The 'mersenne-twister' npm package is a JavaScript implementation of the Mersenne Twister algorithm, which is a pseudorandom number generator (PRNG). It is widely used for generating high-quality random numbers in various applications, including simulations, games, and statistical sampling.
Basic Random Number Generation
This feature allows you to generate a random number between 0 and 1. The Mersenne Twister algorithm ensures that the numbers are uniformly distributed and have a long period, making them suitable for various applications.
const MersenneTwister = require('mersenne-twister');
const generator = new MersenneTwister();
const randomNumber = generator.random();
console.log(randomNumber);
Seeding the Generator
You can seed the Mersenne Twister generator with a specific value to ensure reproducibility of the random number sequence. This is useful for debugging and testing purposes.
const MersenneTwister = require('mersenne-twister');
const seed = 12345;
const generator = new MersenneTwister(seed);
const randomNumber = generator.random();
console.log(randomNumber);
Generating Random Integers
This feature demonstrates how to generate random integers within a specified range. By scaling and flooring the output of the generator, you can obtain random integers suitable for various applications.
const MersenneTwister = require('mersenne-twister');
const generator = new MersenneTwister();
const randomInt = Math.floor(generator.random() * 100);
console.log(randomInt);
The 'random-js' package provides a variety of random number generators, including the Mersenne Twister. It offers more flexibility and additional features such as generating random booleans, picking random elements from an array, and shuffling arrays. Compared to 'mersenne-twister', 'random-js' is more versatile and feature-rich.
The 'seedrandom' package allows you to create seeded random number generators with various algorithms, including the Mersenne Twister. It supports multiple PRNG algorithms and provides a consistent API for generating random numbers. 'seedrandom' is more focused on providing a variety of algorithms and seeding options compared to 'mersenne-twister'.
The 'pure-rand' package offers a collection of pure random number generators, including the Mersenne Twister. It is designed to be fast and efficient, with a focus on functional programming. 'pure-rand' provides a more modern and functional approach to random number generation compared to 'mersenne-twister'.
Mersenne Twister pseudorandom number generator.
Origin source (generator interface was changed)
Algorithm - http://en.wikipedia.org/wiki/Mersenne_twister
$ npm install mersenne-twister
var MersenneTwister = require('mersenne-twister');
var generator = new MersenneTwister();
// Generates a random number on [0,1) real interval (same interval as Math.random)
generator.random();
// [0, 4294967295]
generator.random_int();
// [0,1]
generator.random_incl();
// (0,1)
generator.random_excl();
// [0,1) with 53-bit resolution
generator.random_long();
// [0, 2147483647]
generator.random_int31();
If you want to use a specific seed in order to get a repeatable random sequence, pass an integer into the constructor:
var generator = new MersenneTwister(123);
and that will always produce the same random sequence.
Also you can do it on existing generator instance:
generator.init_seed(123);
See source
FAQs
Mersenne twister pseudorandom number generator
The npm package mersenne-twister receives a total of 268,396 weekly downloads. As such, mersenne-twister popularity was classified as popular.
We found that mersenne-twister demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.