Skip to content

alex7kom/golden-number

Repository files navigation

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

About

A tiny Golden ratio based number generator

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published