{"id":10618,"library":"chance","title":"Chance.js Random Data Generator","description":"Chance.js is a comprehensive utility library designed for generating a wide variety of random data in JavaScript environments, including Node.js and browsers. It currently stands at stable version 1.1.13 and has a generally infrequent release cadence, with major updates occurring as needed for new features or critical bug fixes rather than on a fixed schedule. Its core differentiator is its foundation on the Mersenne Twister algorithm, which allows for repeatable pseudo-random sequences when seeded, making it invaluable for testing, data simulation, and reproducible scenarios. The library provides generators for basic types like numbers, characters, and strings, as well as complex data such as names, addresses, dice rolls, and geographical coordinates, distinguishing itself by its breadth and optional determinism.","status":"active","version":"1.1.13","language":"javascript","source_language":"en","source_url":"https://github.com/chancejs/chancejs","tags":["javascript","chance","random","generator","test","mersenne","name","address","dice"],"install":[{"cmd":"npm install chance","lang":"bash","label":"npm"},{"cmd":"yarn add chance","lang":"bash","label":"yarn"},{"cmd":"pnpm add chance","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The primary export is a constructor function for the Chance class. Instantiate it with `new Chance()` to get a generator instance. The `import chance from 'chance'` form would be correct if it were an already instantiated default export, which it is not.","wrong":"import { Chance } from 'chance';\nimport chance from 'chance';","symbol":"Chance","correct":"import Chance from 'chance';"},{"note":"This is the standard CommonJS import pattern. Remember to instantiate the Chance constructor with `new Chance()`.","wrong":"import Chance from 'chance';","symbol":"Chance","correct":"const Chance = require('chance');"},{"note":"In browser environments, if loaded via a script tag, 'Chance' becomes a global variable. No explicit import statement is needed or supported in this context without a module bundler.","wrong":"import Chance from 'chance'; // In browser without build step","symbol":"Chance (global)","correct":"<script src=\"path/to/chance.min.js\"></script>\n// Chance is now available globally"}],"quickstart":{"code":"import Chance from 'chance';\n\n// Create a new Chance instance (can be seeded for repeatable results)\nconst chance = new Chance(process.env.RANDOM_SEED ?? undefined);\n\nconsole.log('Generating some random data:');\nconsole.log(`- Random Name: ${chance.name()}`);\nconsole.log(`- Random Age: ${chance.age()}`);\nconsole.log(`- Random GUID: ${chance.guid()}`);\nconsole.log(`- Random Sentence: ${chance.sentence()}`);\nconsole.log(`- Random Email: ${chance.email()}`);\nconsole.log(`- Random IPv4 Address: ${chance.ip()}`);\nconsole.log(`- Random Dice Roll (d6): ${chance.d6()}`);\nconsole.log(`- Random Boolean: ${chance.bool()}`);\nconsole.log(`- Random Color (hex): ${chance.color({ format: 'hex' })}`);\nconsole.log(`- Pick a random element: ${chance.pickone(['apple', 'banana', 'cherry'])}`);\n\n// Demonstrating seeding for reproducibility\nconst repeatableChance = new Chance('my-seed-value');\nconst firstString = repeatableChance.string();\nconst secondString = repeatableChance.string();\n\nconst anotherRepeatableChance = new Chance('my-seed-value');\nconst thirdString = anotherRepeatableChance.string();\n\nconsole.log('\\nDemonstrating reproducibility with seeding:');\nconsole.log(`- First string with seed: ${firstString}`);\nconsole.log(`- Second string with seed: ${secondString}`);\nconsole.log(`- Third string with same seed (should match first): ${thirdString}`);\nconsole.log(`- Are first and third strings identical? ${firstString === thirdString}`);","lang":"typescript","description":"This quickstart demonstrates how to instantiate Chance, generate various types of random data, and critically, how to use seeding to achieve reproducible random sequences for testing or deterministic data generation."},"warnings":[{"fix":"Use `const chance = new Chance();` instead of `const chance = Chance();`.","message":"When using Chance.js, always instantiate it with `new Chance()` to get a generator instance. Calling `Chance()` directly without `new` will result in a TypeError because it's a constructor.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For non-reproducible randomness, instantiate without a seed: `const chance = new Chance();`. For environment-driven seeding, use `new Chance(process.env.RANDOM_SEED ?? undefined);`.","message":"To ensure true randomness or unique sequences across different runs in a production environment, avoid hardcoding a seed or remove seeding entirely. Seeding is primarily for reproducible testing.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For ESM projects, use `import Chance from 'chance';`. For CommonJS projects, use `const Chance = require('chance');`. Configure your `package.json` `type` field and build tools appropriately.","message":"While Chance.js supports both CommonJS and ESM, mixing import styles (e.g., `require` in an ESM module or `import` in a CJS module without proper transpilation) can lead to module resolution errors. Ensure your project's module system is consistently configured.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Use `const chance = new Chance();` to instantiate the Chance generator.","cause":"Attempting to call `Chance()` directly without the `new` keyword.","error":"TypeError: Chance is not a constructor"},{"fix":"Ensure you have `import Chance from 'chance';` (ESM) or `const Chance = require('chance');` (CJS) at the top of your file, or that the `<script>` tag is loaded in a browser environment.","cause":"The Chance library was not imported or included correctly, or you are trying to use a CommonJS `require` in an ES Module context without proper setup, or vice versa.","error":"ReferenceError: Chance is not defined"},{"fix":"Change the import to `import Chance from 'chance';`. If you truly need CommonJS, ensure your file is treated as CJS (e.g., using `.cjs` extension or `type: 'commonjs'` in `package.json`).","cause":"Trying to use `require('chance')` in an ES Module context (e.g., in a file where `type: 'module'` is set in `package.json` or a `.mjs` file).","error":"ReferenceError: require is not defined"}],"ecosystem":"npm"}