@bluehive/random

Cryptographically strong random number generation for Node.js and browsers. Ported from Meteor's Random package for full compatibility with modern JavaScript applications.

npm install @bluehive/random

Installation

Package Manager

npm install @bluehive/random
yarn add @bluehive/random

Browser CDN

<script src="https://cdn.jsdelivr.net/npm/@bluehive/random@latest/dist/browser.min.js"></script>

This exposes window.Random globally. For unminified version, use browser.js instead.

Basic Usage

Import the Library

import { Random } from '@bluehive/random';

// Or using CommonJS
const { Random } = require('@bluehive/random');

Key Features

  • Cryptographically strong randomness
  • Cross-platform (Node.js & Browser)
  • TypeScript support
  • Zero dependencies

Available Functions

Random.id()

Generate a unique identifier that's likely to be unique in the whole world.

// Default length: 17 characters
const id = Random.id();
// → "Jjwjg6gouWLXhMGKW"

// Custom length
const shortId = Random.id(10);
// → "3kRtz8fGd2"

Try it out:

Click a button to generate an ID

Random.secret()

Generate a cryptographically secure secret with 6 bits of entropy per character.

// Default length: 43 characters (256 bits)
const secret = Random.secret();
// → "dWx8fQ_kNZ4mK3oT9qZ..."

// Custom length
const shortSecret = Random.secret(20);
// → "mK3oT9qZdWx8fQ_kNZ4m"

Try it out:

Click a button to generate a secret

Random.fraction()

Generate a random number between 0 and 1, like Math.random() but cryptographically secure.

const fraction = Random.fraction();
// → 0.7851294...

// Use for custom ranges
const range = Random.fraction() * 100;
// → 78.51294... (0-100)

Try it out:

Click a button to generate a number

Random.hexString()

Generate a random string of hexadecimal digits.

// Generate 8 hex digits
const hex = Random.hexString(8);
// → "a1b2c3d4"

// Generate 16 hex digits
const longHex = Random.hexString(16);
// → "a1b2c3d4e5f6a7b8"

Try it out:

Click a button to generate hex

Random.choice()

Randomly select an element from an array or string.

// Choose from array
const colors = ['red', 'green', 'blue'];
const color = Random.choice(colors);
// → "green"

// Choose from string
const char = Random.choice('ABCDEF');
// → "C"

Try it out:

Click a button to make a choice

Advanced Usage

Seeded Random Generation

Create deterministic random generators for testing and reproducible results.

// Create seeded generator
const seeded = Random.createWithSeeds('seed1', 'seed2');

// Always produces same sequence
const id1 = seeded.id();
const id2 = seeded.id();

// Create another with same seeds
const seeded2 = Random.createWithSeeds('seed1', 'seed2');
// seeded2.id() === id1 (same sequence)

Demo:

Click to see deterministic generation

Fast Non-Cryptographic Generation

Use the insecure generator for better performance when cryptographic security isn't required.

// Use insecure but fast generator
const fastId = Random.insecure.id();
const fastSecret = Random.insecure.secret(10);
const fastNumber = Random.insecure.fraction();

// Good for simulations, games, etc.
// NOT suitable for security purposes

Performance Test:

Click to run performance comparison

Interactive Playground

Custom Function Builder

Output & Code

Random.id(17)
Click execute to see result