Skip to content

RetroVX/random

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Random

A Tiny Random Utility Generator

License: MIT

Features

  • Alea RNG
  • Seedable
  • Random
    • number
    • between
    • chance
    • itemFromArray
    • entryFromObject
    • keyFromObject
  • Tested with Jest
  • 2kb minified (<1kb minified + gzipped)

Install

git clone https://github.com/RetroVX/random.git

Or download from Zip

Node & Common

const Random = require('./path/to/random.js');

const seed = 'mySeed';
const random = new Random(seed);

Module

import Random from "./path/to/random.mjs";

const seed = 'mySeed';
const random = new Random(seed);

UMD

<script type="text/javascript" src="path/to/random.umd.js"></script>
const seed = 'mySeed';
const random = new window.random(seed);

Examples

// generate a random number between 0 and 1.
// use this in place of Math.random()
random.gen(); -> 0.9401234467513859

// generate a random number with a max number (defaults to 100 if undefined)
random.number(100); -> 42

// generates a random number using a min and max number
random.between(1, 25); -> 17

// percent chance to happen
random.chance(50) -> true

// picks a random item from an array
random.itemFromArray([1, 2, 3, 4, 5]); -> 2

// picks a random key/value pair and outputs as an array
random.entryFromObject({prop1: 'hello', prop2: 'world'}); -> ['prop1', 'hello']

// picks a random key from an object
random.keyFromObject({prop1: 'hello', prop2: 'world'}); -> 'prop2'

// new seed
random.newGenerator(seed);

Credit

Alea

Version 1.3.0

Releases

No releases published

Packages

No packages published