Vegas

Generate random numbers and samples.

Vegas helps do things based on randomness. For example picking a random integer, picking a random element from a list, or even collecting a unique sample of elements from a list. This library takes inspiration from Python’s random library.

Usage

import { randomInt } from "https://deno.land/x/vegas@1.0.0/mod.ts";

console.log(randomInt(0, 4)); // 2

API

Below are examples for all currently available APIs. Except for the makeGenerators and makeSeededGenerators functions, all functions draw randomness from Math.random. Use the aforementioned functions to draw randomness from a different source.

// int from and including two, up to and excluding eight.
randomInt(2, 8);

// int below 4.
randomBelow(4);

// pick a random element from a list.
randomPick([1, 2, 3]);

// pick a sample of two elements from a list.
randomSample([1, 2, 3, 4], 2);

// random float, mostly here for seeded random float generation.
randomFloat();

// initializes all random generators with a seeded random number generator.
const vegas = makeSeededGenerators("my-seed");
vegas.randomInt(2, 32);

// initializes all random generators with a custom random number generator.
const vegasCustom = makeGenerators(Math.random);
vegasCustom.randomInt(0, 10);