Giter Club home page Giter Club logo

randomly's Introduction

Randomly

A utility for generating lots of random numbers without being too tasking on the browser. Randomly accomplishes this by generating a set of random numbers on a given interval.

Subsequent requests for random numbers are served from the generated set instead of calling Math.random. In addition to getting random numbers, some useful methods have also been added around random numbers.

Motivation

Working on several generative art projects inspired the need for a common utility which would handle very frequent calls to Math.random as well as other provided methods which involve using random numbers.

Benchmarks

Why not just use Math.random? Depending on how many numbers are being generated Randomly tends to be more performant as frequency increases. The following chart shows their comparison. You can view how this was measured in the benchmark.ts file.

Usage

import Randomly from '@ededejr/randomly';

const randomly = new Randomly();

Getting a random number

randomly.get(); // Returns a number between 0 and 1

Compare a random number using a predicate

randomly.compare((value) => Math.floor(value * 1000) > 500);

Check if a random number is less than a threshold

randomly.lt(0.2);

Check if a random number is greater than a threshold

randomly.gt(0.2);

Check if a random number is between an upper and lower bound

randomly.between(0.2, 0.6);

Decide between two values

randomly.decide('Option A', 'Option B');

Get a Radom number between 0 and a given value

randomly.sample(100);

Controlling the timer

If you would like to pause the interval:

randomly.stopTimer();

Starting it up again:

randomly.startTimer();

Customizing the interval or store size

import Randomly from '@ededejr/randomly';

const randomly = new Randomly({
	refreshInterval: 5000, // refresh random number store every 5 seconds
	storeSize: 100, // keep a store of a 100 random numbers
});

randomly's People

Contributors

dependabot[bot] avatar ededejr avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.