1.0.1 • Published 8 years ago

random-floating v1.0.1

Weekly downloads
67
License
MIT
Repository
github
Last release
8 years ago

random-floating

Return a random floating point number.

MIT License

build:? coverage:?

Install

$ npm install --save random-floating

Usage

For more use-cases see the tests

var randomFloat = require('random-floating');

// API
// - randomFloat([options])

// options
// - min
// - max
// - fixed

By default it will return a fixed number of at most 4 digits after the decimal.

randomFloat();
// range: -9007199254740992 to 9007199254740992
// => -211920142886.5024

Note: at most 4 digits. This because, unless we returned trailing zeroes (which aren’t allowed on the JavaScript float) we can’t guarantee 4 digits after the decimal. So if random chance comes back with 82383854.2000 then 82383854.2 is what will be returned.

Can optionally provide min and max:

randomFloat({max: 5});
// => -239837235632.3785

randomFloat({min:10, max: 100});
// => 66.7269

Note: these min and max are inclusive, so they are included in the range.

To retrieve a set number of fixed digits after the decimal, provide it as an option:

randomFloat({fixed: 7});
// => -749512327.7447168

Or combine them:

randomFloat({min: 0, max: 100, fixed: 8});
// => 45.92367599

Related

Contributing

Pull requests and stars are highly welcome.

For bugs and feature requests, please create an issue.