Introduction
Chance.js is a minimalist random data generator for JavaScript. It produces realistic random values for names, addresses, phone numbers, dates, strings, and many other data types, making it useful for testing, prototyping, and seeding development databases.
What Chance.js Does
- Generates random personal data: names, emails, phone numbers, addresses, and SSNs
- Produces random primitives: integers, floats, booleans, strings, and characters
- Creates random dates, timestamps, and time-related values
- Provides domain-specific generators for colors, coordinates, IP addresses, and credit cards
- Supports seeded randomness for reproducible test data across runs
Architecture Overview
Chance.js is a single-file library with no external dependencies. It uses a Mersenne Twister pseudorandom number generator internally, which provides high-quality distribution and supports seeding. All generator methods are attached to a Chance instance, and each instance can be independently seeded. The library is designed for both browser and Node.js environments.
Self-Hosting & Configuration
- Install via npm or include directly via a script tag in the browser
- Create a Chance instance with an optional seed for reproducible output
- Call generator methods with optional configuration objects to constrain output
- Use chance.mixin() to register custom generator functions
- Supports UMD, CommonJS, and ES module import styles
Key Features
- Seeded random number generator for deterministic, reproducible test data
- Wide variety of built-in generators covering personal data, finance, geography, and web
- Zero dependencies with a small footprint suitable for browser and server
- Mixin system for extending with custom data generators
- Configurable output ranges and formats for most generator methods
Comparison with Similar Tools
- Faker.js — larger library with locale support and more data types; Chance.js is lighter with built-in seeding
- casual — similar scope but less actively maintained; Chance.js has broader generator coverage
- falso — modern Faker alternative with tree-shaking; Chance.js offers a simpler single-instance API
- randexp — generates strings matching a regex; Chance.js provides structured data types, not pattern-based
- Fishery — factory-based test data with associations; Chance.js focuses on individual random values
FAQ
Q: How do I generate the same random data across test runs? A: Pass a seed to the constructor: new Chance(42). The same seed always produces the same sequence of values.
Q: Can I use Chance.js in the browser? A: Yes. Include it via a script tag or import it with a bundler. It has no Node.js-specific dependencies.
Q: How do I add custom generators? A: Use chance.mixin({ myGenerator: function() { return ...; } }) to add new methods to the instance.
Q: Does Chance.js support internationalized data? A: Chance.js focuses on English-language data. For localized names and addresses, consider Faker.js which has extensive locale support.