Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/alextes/vegas

Vegas helps generate numbers or pick elements based on randomness.
https://github.com/alextes/vegas

deno deno-module denoland random random-generation

Last synced: 2 days ago
JSON representation

Vegas helps generate numbers or pick elements based on randomness.

Awesome Lists containing this project

README

        

Vegas


Generate random numbers and samples.














dependency count
dependencies up-to-date

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](https://docs.python.org/3/library/random.html).

## Usage

```ts
import { randomInt } from "https://deno.land/x/[email protected]/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.

```ts
// 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);
```