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

random-browser

v1.1.0

Published

The random module is used for generating cryptographically strong random numbers suitable for managing data such as passwords, account authentication, security tokens, and related secrets.

Downloads

29

Readme

random-browser-js

CI pre-commit.ci status codecov

This module is used for generating cryptographically strong random numbers suitable for managing data such as passwords, account authentication, security tokens, and related secrets.

In particular, this should be used in preference to the default pseudo-random number generator Math.random(), which is designed for modelling and simulation, not security or cryptography.

Inspired by crypto from Node.js and secrets from Python.

Example

<script type="module">
import {
  choice,
  randomBits,
  randomBytes,
  randomInt,
  tokenHex,
  tokenUrlsafe,
  uuidv7
} from 'https://cdn.jsdelivr.net/npm/random-browser';

console.log('Pick a random fruit from array: ' + choice(['Apple', 'Banana', 'Orange']));
console.log('Pick a random character from string: ' + choice('ABCDEF'));
console.log('Random integer with 4 random bits (<16): ' + randomBits(4));
console.log('3 random bytes e.g. [218, 82, 127]: ' + randomBytes(3));
console.log('Random number chosen from (0, 1, 2): ' + randomInt(3));
console.log('The dice rolled: ' + randomInt(1, 7));
console.log('32 character hexadecimal string from 16 random bytes: ' + tokenHex(16));
console.log('URL-safe Base64 text string from 32 random bytes: ' + tokenUrlsafe());
console.log('UUID Version 7: ' + uuidv7());
</script>

Install

Use a CDN like JSDelivr:

https://cdn.jsdelivr.net/npm/[email protected]
or
https://cdn.jsdelivr.net/npm/random-browser

Or you can download random.js from GitHub. Alternatively, you can install it via npm:

npm install random-browser

Features

  • No dependencies or huge compiled files.
  • No configuration.
  • No need to choose between entropy sources.
  • Minimizing footguns is a high priority.
  • Just call the functions and you're all set.

Usage

choice(arr)

  • arr <Array> The array containing the choices.
  • Returns <Object>

Return a randomly-chosen element from a non-empty array arr.

choice(['Apple', 'Banana', 'Orange']);
choice('ABCDEF');

randomBits(k)

  • k <integer> Number of bits.
  • Returns <integer>

Return a random integer n with k random bits such that 0 <= n < 2k. The number of bits (k) must be less than or equal to 48.

randomBits(4);

randomBytes(size)

  • size <integer> The number of bytes to generate.
  • Returns: <Uint8Array>

Generates cryptographically strong pseudorandom data. The size argument is a number indicating the number of bytes to generate.

randomBytes(3);

randomInt([min, ]max)

  • min <integer> Start of random range (inclusive). Default: 0.
  • max <integer> End of random range (exclusive).
  • Returns <integer>

Return a random integer n such that min <= n < max. The range (max - min) must be less than 248. min and max must be safe integers.

randomInt(3);
randomInt(1, 7);

tokenHex([numBytes])

  • numBytes <integer> The number of bytes to generate. Default: 32.
  • Returns: <string>

Return a random text string, in hexadecimal. The string has numBytes random bytes, each byte converted to two hex digits. If numBytes is not supplied, a reasonable default is used.

tokenHex();
tokenHex(16);

tokenUrlsafe([numBytes])

  • numBytes <integer> The number of bytes to generate. Default: 32.
  • Returns: <string>

Return a random URL-safe text string, containing numBytes random bytes. The text is Base64 encoded, so on average each byte results in approximately 1.3 characters. If numBytes is not supplied, a reasonable default is used.

tokenUrlsafe();
tokenUrlsafe(16);

uuidv7()

  • Returns: <string>

Return a UUID Version 7 in the 8-4-4-4-12 canonical hexadecimal string representation.

uuidv7();