API Reference

Chance

Random data generator producing strings, numbers, names, and dates with seeded reproducibility for tests.

Official Documentation ↗
CompatibilityThis documentation covers version v1. Check the official docs for the latest API surface.

Installation

npmnpm install chance
pnpmpnpm add chance

Methods

Overview

Chance is a random data generator for strings, numbers, names, and dates with seeded reproducibility for tests. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.

Core APIs

  • chance.string — Produces a random string, optionally constrained to a length or character pool.
  • chance.integer — Returns a random integer within the inclusive bounds you provide.
  • chance.pickone — Selects one element from an array uniformly at random.

Example

const Chance = require('chance');
const chance = new Chance();

console.log(chance.string({ length: 8, pool: 'abcdef' }));

Notes

Seeded instances give deterministic sequences for fixtures. Never use it for security tokens because the generator is not cryptographically secure.

Verification

Create two seeded instances with the same seed and confirm they produce an identical sequence of values.

FAQ

How do I get reproducible random data?
Create an instance with a seed, new Chance("seed"), so the same sequence is produced on every run.
Can I localize generated names?
Pass a locale such as new Chance("", { locale: "de" }) to change the person and location vocabularies.
Is Chance suitable for security tokens?
No. Its PRNG is not cryptographically secure; use crypto.randomBytes for tokens and secrets.