Git Product home page Git Product logo

golden-number's Introduction

golden-number

A tiny Golden ratio based number generator

Only 49 bytes minified and gzipped

npm install golden-number

then

const createGenerator = require('golden-number');

const generator = createGenerator();
const num = generator(); //=> 0.70693204169029

How & why

Each produced value is a modulo 1 of the sum of the previous value (or a seed) and the golden ratio. Values are uniformly distributed but appear in a predictable repeatable pattern where neighbor values can't appear one after another. This can be used for generating a diverse set of values, for example we can use generated values for hue in HSL for generating colors. Compare it to a plain Math.random():

element.style.backgroundColor = `hsl(${Math.round(randomValue * 360)}, 83%, 71%)`;

generated colors: golden-number vs. Math.random()

API

createGenerator(seed?: number): () => number

Create Golden ratio based number generator function.

Returns a function that will return a new number on each call.

  • seed - A float number between 0 and 1. Default is a random number from Math.random()

Acknowledgments

Inspired by How To Generate Random Colors Programmatically.

License

MIT

golden-number's People

Contributors

alex7kom avatar

Stargazers

 avatar  avatar  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.