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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@alwatr/random

v5.1.28

Published

A lightweight utility library for generating random numbers, strings, UUIDs and more

Readme

@alwatr/random

A lightweight, high-performance utility library for generating random numbers, strings, UUIDs and more.

Installation

yarn add @alwatr/random
# or
npm install @alwatr/random

Features

  • Cross-platform (Node.js and browsers)
  • TypeScript support with type safety
  • Zero dependencies (except for internal package tracer)
  • Uses cryptographic random when available
  • Comprehensive set of random utilities
  • Supports tree-shaking with individually exported functions

Usage

import {
  randNumber,
  randInteger,
  randFloat,
  randString,
  randStep,
  randShuffle,
  randPick,
  randUuid,
  randBoolean,
  randColor,
  randArray,
  bytesToHex,
} from '@alwatr/random';

// Get random number between 0 and 1
console.log(randNumber()); // 0.7124123

// Get random integer in range
console.log(randInteger(1, 10)); // 7

// Get random float in range
console.log(randFloat(1, 10)); // 7.214124

// Get random string with fixed length
console.log(randString(6)); // "A1b2C3"

// Get random string with variable length
console.log(randString(3, 6)); // "X2fg"

// Get random string with custom character set
console.log(randString(5, undefined, '01')); // "10101"

// Get random value with step
console.log(randStep(0, 10, 2)); // 0, 2, 4, 6, 8, or 10

// Shuffle an array
const array = [1, 2, 3, 4, 5];
console.log(randShuffle([...array])); // [3, 1, 5, 2, 4]

// Pick a random item from array
console.log(randPick(array)); // 3

// Generate UUID
console.log(randUuid()); // "a1b2c3d4-e5f6-47a8-b9c0-d1e2f3a4b5c6"

// Generate random boolean
console.log(randBoolean()); // true or false (50% chance)
console.log(randBoolean(0.8)); // true or false (80% chance of true)

// Generate random color
console.log(randColor()); // "#a1b2c3"

// Convert bytes to hexadecimal string
const bytes = new Uint8Array([10, 255, 0, 16]);
console.log(bytesToHex(bytes)); // "0aff0010"

API

randNumber()

Returns a random float between 0 and 1 (not including 1). This is a direct wrapper around Math.random().

const value = randNumber(); // 0.7124123

randInteger(min, max)

Returns a random integer between min and max (inclusive).

const value = randInteger(1, 10); // Between 1 and 10

randFloat(min, max)

Returns a random float between min and max (max not included).

const value = randFloat(1, 10); // Between 1 and 10 (float)

randString(min, max?, chars?)

Generates a random string with characters from the specified character set (defaults to alphanumeric set: A-Z, a-z, 0-9).

  • With one argument: Returns a string of exactly that length
  • With two arguments: Returns a string with random length between min and max (inclusive)
  • With three arguments: Uses the provided character set instead of the default alphanumeric set
const fixedLength = randString(6); // "A1b2C3"
const variableLength = randString(3, 6); // Random length between 3 and 6
const binaryString = randString(8, undefined, '01'); // "10110010"
const hexString = randString(6, 6, '0123456789abcdef'); // "a3f28c"

randStep(min, max, step)

Returns a random number between min and max with the specified step.

const value = randStep(0, 10, 2); // 0, 2, 4, 6, 8, or 10

randShuffle(array)

Shuffles an array in place using Fisher-Yates algorithm and returns it.

const array = [1, 2, 3, 4, 5];
randShuffle(array); // [3, 1, 5, 2, 4]

randPick(array)

Returns a random element from an array.

const array = [1, 2, 3, 4, 5];
const value = randPick(array); // One random element

randUuid()

Generates a random UUID v4. Uses crypto.randomUUID() when available, with a fallback implementation.

const id = randUuid(); // "a1b2c3d4-e5f6-47a8-b9c0-d1e2f3a4b5c6"

randBoolean(probability?)

Generates a random boolean with the specified probability of being true (default is 0.5).

const value = randBoolean(); // true or false (50% chance)
const mostlyTrue = randBoolean(0.8); // true or false (80% chance of true)

randColor()

Generates a random hex color string with 6 hexadecimal digits (representing RGB values).

const color = randColor(); // "#a1b2c3"

randArray(array, min?, max?)

Fills a typed array with random integer values within the specified range. The array is modified in place and also returned for chaining.

// Fill a Uint8Array with random values (0-255)
randArray(new Uint8Array(10));

// Fill with custom range
randArray(new Uint16Array(5), 1000, 2000); // Values between 1000-2000

// Also works with number arrays
randArray(new Array<number>(8), -100, 100); // Values between -100 and 100

bytesToHex(bytes)

Converts a Uint8Array or number array into a hexadecimal string representation. Each byte is converted to a two-character hex string (padded with a leading zero if necessary) and concatenated together.

const bytes = new Uint8Array([10, 255, 0, 16]);
const hex = bytesToHex(bytes); // "0aff0010"

const array = [171, 205, 3];
const hex2 = bytesToHex(array); // "abcd03"

Sponsors

The following companies, organizations, and individuals support Nanolib ongoing maintenance and development. Become a Sponsor to get your logo on our README and website.

Contributing

Contributions are welcome! Please read our contribution guidelines before submitting a pull request.