npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pure-random-number

v3.2.1

Published

A cryptographically secure generator for random numbers in a range.

Downloads

129

Readme

pure-random-number

A module for generating cryptographically secure pseudo-random numbers.

Zero dependencies ~56LOC, pure es6/esm, runs on browser + node without transpilation.

This module is based on code originally written by Scott Arciszewski, (WTFPL / CC0 / ZAP)

Usage

Basic:

import { randomNumber } from 'pure-random-number'

const n = await randomNumber(1, 20) // defaults to crypto:subtle getRandomValues()
console.log('Cryptographically Secure Random Number', n)

Bring your own generator:

let prevHash = await sha256('Hello World')
const prng = async (nBytes) => {
    if (nBytes > 32) throw new Error('Entropy unsupported')
    prevHash = await sha256(prevHash)
    return prevHash
}

const n = await randomNumber(1, 20, prng)

console.log('Deterministic Random Number', n)

API

see JSdoc annotations for accurate descriptions.

await randomNumber(minimum: number, maximum: number, generator = randomBytes)

Returns a stable random positive integer within the specified range.

Note that the range is inclusive, and both numbers must be positive integer values.

It is not possible to securely generate a random value for floating point numbers, so if you are working with fractional numbers (eg. 1.24), you will have to decide on a fixed 'precision' and turn them into integer values (eg. 124).

bytesNeeded(min: number, max: number)

Returns amount of bytes required for a given range

randomSeedNumber(seed: Uint8Array, minimum: number, maximum: number)

Returns a stable random positive integer extracted from seed within the specified range.

Changelog

  • 3.2.0 (July 7, 2024) Removed obsolete validators, exported sync randomSeedNumber() & bytesNeeded()
  • 3.0.0 (July 7, 2024): Converted to ESM, removed sync version
  • 2.1.0 (June 12, 2020): Added sync version.
  • 2.0.0 (May 3, 2020): Removed dependencies and ported to standardjs
  • 1.0.2 (March 8, 2016): Security release! Patched handling of large numbers; input values are now checked for MIN_SAFE_INTEGER and MAX_SAFE_INTEGER, and the correct bitwise operator is used (>>> rather than >>).
  • 1.0.1 (March 8, 2016): Unimportant file cleanup.
  • 1.0.0 (March 8, 2016): Initial release.

License

Apache 2.0 - Decent Labs