Pandemonium
Pandemonium is a lightweight JavaScript and TypeScript library providing a collection of common random-related utility functions. Currently stable at version 2.4.1, it offers functionalities such as `choice` for selecting random items, `random` for generating numbers within a range, `shuffle` for array randomization, and various specialized sampling algorithms like `reservoirSample` and `geometricReservoirSample`. A key differentiator is its modular design, allowing users to create custom versions of any function by injecting a specific random number generator (RNG) source, enabling seeded or otherwise controlled randomness. The library emphasizes performance for its sampling methods, offering detailed complexity analysis. Its release cadence is not explicitly stated in the provided documentation, but the package appears actively maintained with regular updates.
Common errors
-
TypeError: (0 , _pandemonium_choice__WEBPACK_IMPORTED_MODULE_0__.default) is not a function
cause Attempting to import a named export as a default export, or vice-versa, especially when importing from the main `pandemonium` package.fixIf importing from `pandemonium`, use named imports: `import { choice } from 'pandemonium';`. If importing from a subpath (e.g., `pandemonium/choice`), check if it's a default export: `import choice from 'pandemonium/choice';`. -
SyntaxError: Cannot use import statement outside a module
cause Trying to use ES Module `import` syntax in a CommonJS (`.js` without `"type": "module"` in `package.json`) Node.js environment.fixEnsure your Node.js project is configured for ES Modules by adding `"type": "module"` to your `package.json`, or rename your file to `.mjs`. Alternatively, if targeting older Node.js or a specific CJS environment, you might need a bundler/transpiler like Babel or Webpack. -
Property 'createChoice' does not exist on type 'typeof import("pandemonium")'.cause Incorrectly trying to import a 'create' function (e.g., `createChoice`) directly from the main `pandemonium` entry point instead of its specific subpath.fixImport 'create' functions from their dedicated subpaths, e.g., `import { createChoice } from 'pandemonium/choice';`.
Warnings
- gotcha The `dangerouslyMutatingSample` function modifies the input array directly for performance. This can lead to unexpected side effects if the original array is still needed or referenced elsewhere in your application.
- gotcha The `fisherYatesSample` method is explicitly noted in the documentation as 'Probably not a good idea.', indicating it may have performance or memory efficiency drawbacks compared to other sampling algorithms offered by the library.
- gotcha While the package's `package.json` includes a `main` field pointing to a CommonJS entry, the documentation primarily showcases ES Module (`import`) syntax. Relying on `require()` directly may lead to unexpected behavior or require specific transpilation setups in modern Node.js environments.
- gotcha The library's documentation does not explicitly detail breaking changes between major versions. Developers upgrading Pandemonium should proactively consult the project's changelog or GitHub releases to identify potential breaking changes, especially when moving to a new major version.
Install
-
npm install pandemonium -
yarn add pandemonium -
pnpm add pandemonium
Imports
- choice
const choice = require('pandemonium/choice');import choice from 'pandemonium/choice';
- choice
import choice from 'pandemonium'; // The main 'pandemonium' entry point does not offer a default export.
import { choice } from 'pandemonium'; - createRandom
import { createRandom } from 'pandemonium'; // Creator functions are specific to their utility's subpath.import { createRandom } from 'pandemonium/random'; - shuffle
import shuffle from 'pandemonium/shuffle'; // While valid for direct subpath default import, named from main is often preferred.
import { shuffle } from 'pandemonium'; - RandomBooleanFunction
import type { RandomBooleanFunction } from 'pandemonium/random-boolean';
Quickstart
import { choice, random, shuffle } from 'pandemonium';
import { createRandom } from 'pandemonium/random';
import seedrandom from 'seedrandom'; // Ensure 'seedrandom' is installed: npm install seedrandom
// Basic usage of random utilities
const fruits = ['apple', 'banana', 'cherry', 'date'];
console.log('Random fruit:', choice(fruits));
const randomNumber = random(10, 20); // Integer between 10 and 20 (inclusive)
console.log('Random number (10-20):', randomNumber);
const numbersToShuffle = [1, 2, 3, 4, 5];
const shuffledNumbers = shuffle(numbersToShuffle); // Returns a new shuffled array
console.log('Shuffled numbers:', shuffledNumbers);
// Demonstrating custom RNG for reproducible results
const seed = 'my_secret_seed';
const seededRNG = seedrandom(seed); // Create a seeded RNG function
// Create a custom random function using the injected seeded RNG
const reproducibleRandom = createRandom(seededRNG);
console.log(`\nReproducible random numbers with seed "${seed}":`);
for (let i = 0; i < 3; i++) {
console.log(` Run ${i + 1}:`, reproducibleRandom(1, 100));
}
// Verify reproducibility by creating another instance with the same seed
const anotherSeededRNG = seedrandom(seed);
const anotherReproducibleRandom = createRandom(anotherSeededRNG);
console.log(`Reproducible random numbers (second run with same seed "${seed}"):`);
for (let i = 0; i < 3; i++) {
console.log(` Run ${i + 1}:`, anotherReproducibleRandom(1, 100));
}