node-random-int
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

Build Status Coverage Status npm

node-random-int

Cryptographically secure pseudo-random number generator for node.js

The core logic is loosely based on the node-random-number-csprng project by joepie91


Install

npm install node-random-int


Usage

Async:

import {createRandomNumberGenerator} from 'node-random-int';
 
async function FOO(){
  const randomNumberGenerator = createRandomNumberGenerator(0, 100);
  const randomInt1 = await randomNumberGeneraton.nextValueAsync();
  const randomInt2 = await randomNumberGeneraton.nextValueAsync();
}

Can also be consumed via for await loop:

import {createRandomNumberGenerator} from 'node-random-int';
 
async function FOO(){
  const randomNumberGenerator = createRandomNumberGenerator(0, 100);
  
  for await(let randomInt of randomNumberGenerator){
    // this will loop indefinitely!
    // you must implement some kind of break logic
    if(randomInt > 2){
      break;
    }
  }
}

convenience method:

import {getRandomIntegerAsync} from 'node-random-int';
 
async function FOO(){
  // each invocation creates a new randomNumberGenerator object
  const randomInt1 = await getRandomIntegerAsync(0, 100);
  const randomInt2 = await getRandomIntegerAsync(0, 100);
}

Synchronous

import {createRandomNumberGenerator} from 'node-random-int';
 
const randomNumberGenerator = createRandomNumberGenerator(0, 100);
const randomInt1 = randomNumberGeneraton.nextValueSync();
const randomInt2 = randomNumberGeneraton.nextValueSync();

Can be consumed synchronously via for loop:

import {createRandomNumberGenerator} from 'node-random-int';
 
const randomNumberGenerator = createRandomNumberGenerator(0, 100);
for (let randomInt of randomNumberGenerator){
  // this will loop indefinitely!
  // you must implement some kind of break logic
  if(randomInt > 2){
    break;
  }
}

convenience method:

import {getRandomInteger} from 'node-random-int';
 
function FOO(){
  // each invocation creates a new randomNumberGenerator object
  const randomInt1 = getRandomInteger(0, 100);
  const randomInt2 = getRandomInteger(0, 100);
}

Tests

npm run test

Licence

Readme

Keywords

none

Package Sidebar

Install

npm i node-random-int

Weekly Downloads

1

Version

0.0.1

License

ISC

Unpacked Size

20.5 kB

Total Files

6

Last publish

Collaborators

  • ryder_brooks