Security News
The Risks of Misguided Research in Supply Chain Security
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
random-seed
Advanced tools
GRC's UHE PRNG in node (Ultra-High Entropy Pseudo-Random Number Generator by Gibson Research Corporation)
The random-seed npm package is used to generate random numbers based on a seed value. This allows for reproducible random number sequences, which can be useful in scenarios like testing, simulations, and procedural content generation.
Seeded Random Number Generation
This feature allows you to create a random number generator that produces the same sequence of random numbers for a given seed. This is useful for reproducibility in tests and simulations.
const randomSeed = require('random-seed');
const seed = 'my-seed';
const rand = randomSeed.create(seed);
console.log(rand.random()); // Generates a random number based on the seed
Random Integer Generation
This feature allows you to generate a random integer within a specified range, based on the seed. This can be useful for scenarios where you need random but reproducible integers.
const randomSeed = require('random-seed');
const seed = 'my-seed';
const rand = randomSeed.create(seed);
console.log(rand.intBetween(1, 100)); // Generates a random integer between 1 and 100 based on the seed
Random Float Generation
This feature allows you to generate a random floating-point number within a specified range, based on the seed. This can be useful for simulations and procedural content generation.
const randomSeed = require('random-seed');
const seed = 'my-seed';
const rand = randomSeed.create(seed);
console.log(rand.floatBetween(0, 1)); // Generates a random float between 0 and 1 based on the seed
The seedrandom package provides a similar functionality to random-seed, allowing for seeded random number generation. It offers more features, such as the ability to generate random numbers with different algorithms (e.g., Alea, XOR128, etc.). It is also widely used and well-documented.
The random-js package is a versatile random number generator library that supports seeding. It provides a variety of random number generation methods, including integers, floating-point numbers, and booleans. It also supports different distributions, such as uniform and normal distributions.
The chance package is a comprehensive random generator helper for JavaScript. It supports seeded random number generation and offers a wide range of random data generation capabilities, including names, addresses, and other complex data types. It is highly customizable and well-suited for testing and data generation.
Gibson Research Corporation's Ultra-High Entropy Pseudo-Random Number Generator ported to node.
The original library / project page is located here: https://www.grc.com/otg/uheprng.htm
The node project page is here: https://github.com/skratchdot/random-seed
There were a few modifications made to the original library to allow seeding, and to pass jshint.
I've also added the following helper methods:
Install the module with: npm install random-seed
var rand = require('random-seed').create();
var n = rand(100); // generate a random number between 0 - 99
var gen = require('random-seed'); // create a generator
// these generators produce different numbers
var rand1 = gen.create(); // method 1
var rand2 = new gen(); // method 2
var rand3 = gen(); // method 3
// these generators will produce
// the same sequence of numbers
var seed = 'My Secret String Value';
var rand4 = gen.create(seed);
var rand5 = new gen(seed);
var rand6 = gen(seed);
Once a random generator is created, you have the following methods available.
I typically create a random generator like this:
var rand = require('random-seed').create();
Returns a random integer between 0 (inclusive) and range (exclusive)
Returns a random integer between 0 (inclusive) and range (exclusive)
Returns a random float between 0 (inclusive) and 1 (exclusive)
Works the same as Math.random()
Returns a random float between min (inclusive) and max (exclusive)
Returns a random integer between min (inclusive) and max (inclusive)
Same as calling rand.initState() followed by rand.hashString(seed). If seed is not a string, then the seed value will be converted to a string. If you don't pass a seed argument, then the generator uses Math.random() as the seed.
Returns a pseudo-random string of 'count' printable characters ranging from chr(33) to chr(126) inclusive.
Removes leading and trailing spaces and non-printing control characters, including any embedded carriage-return (CR) and line-feed (LF) characters, from any string it is handed. This is also used by the 'hashstring' function (below) to help users always obtain the same EFFECTIVE uheprng seeding key.
Hashes the provided character string after first removing any leading or trailing spaces and ignoring any embedded carriage returns (CR) or Line Feeds (LF).
This handy exported function is used to add entropy to our uheprng at any time.
If we want to provide a deterministic startup context for our PRNG, but without directly setting the internal state variables, this allows us to initialize the mash hash and PRNG's internal state before providing some hashing input.
We use this (optional) exported function to signal the JavaScript interpreter that we're finished using the internal "Mash" hash function so that it can free up the local "instance variables" it will have been maintaining. It's not strictly necessary, of course, but it's good JavaScript citizenship.
var rand = require('random-seed').create();
var n = rand(100); // generate a random number between 0 - 99
var rand = require('random-seed').create();
rand.initState();
var n1 = rand(100); // n1 === 58
var n2 = rand(100); // n2 === 26
rand.initState(); // re-init
var n3 = rand(100); // n3 === 58 && n3 === n1
var rand1 = require('random-seed').create(),
rand2 = require('random-seed').create();
console.log(rand1(100), rand2(100));
var seed = 'Hello World',
rand1 = require('random-seed').create(seed),
rand2 = require('random-seed').create(seed);
console.log(rand1(100), rand2(100));
var math = require('random-seed').create();
console.log(math.random());
Copyright (c) 2013 skratchdot
Dual Licensed under the MIT license and the original Public Domain License by GRC.
FAQs
GRC's UHE PRNG in node (Ultra-High Entropy Pseudo-Random Number Generator by Gibson Research Corporation)
We found that random-seed 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.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
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.