# random-number-csprng

> A cryptographically secure generator for random numbers in a range.

Latest version **1.0.2** (published 2016-03-08) · WTFPL license · 0 weekly downloads

## Install

```sh
npm install random-number-csprng
pnpm add random-number-csprng
yarn add random-number-csprng
bun add random-number-csprng
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2016-03-08 |
| First published | 2016-03-08 |
| Weekly downloads | 0 |
| License | WTFPL |
| TypeScript types | separate (@types/random-number-csprng) |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Sven Slootweg |
| Maintainers | joepie91 |
| Keywords | csprng, random, number, crypto |

## Links

- npm: https://www.npmjs.com/package/random-number-csprng
- Repository: https://github.com/joepie91/node-random-number-csprng
- Homepage: https://github.com/joepie91/node-random-number-csprng#readme
- Issues: https://github.com/joepie91/node-random-number-csprng/issues
- npm.io page: https://npm.io/package/random-number-csprng

## Dependencies (2)

- [bluebird](https://npm.io/package/bluebird.md) ^3.3.3
- [create-error](https://npm.io/package/create-error.md) ^0.3.1

## Alternatives

- [random-seedable](https://npm.io/package/random-seedable.md) — 27.9K weekly downloads
- [n2words](https://npm.io/package/n2words.md) — 22.2K weekly downloads
- [@stdlib/math-base-special-factorialln](https://npm.io/package/@stdlib/math-base-special-factorialln.md) — 5.7K weekly downloads
- [@stdlib/math-base-special-abs2](https://npm.io/package/@stdlib/math-base-special-abs2.md) — 1.7K weekly downloads
- [commons-math-interpolation](https://npm.io/package/commons-math-interpolation.md) — 1.4K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2016-03-08
- 1.0.1 — 2016-03-08

## README

# random-number-csprng

A CommonJS module for generating cryptographically secure pseudo-random numbers.

Works in Node.js, and should work in the browser as well, using Webpack or Browserify.

This module is based on code [originally written](https://gist.github.com/sarciszewski/88a7ed143204d17c3e42) by [Scott Arciszewski](https://github.com/sarciszewski), released under the WTFPL / CC0 / ZAP.

## License

[WTFPL](http://www.wtfpl.net/txt/copying/) or [CC0](https://creativecommons.org/publicdomain/zero/1.0/), whichever you prefer. A donation and/or attribution are appreciated, but not required.

## Donate

My income consists largely of donations for my projects. If this module is useful to you, consider [making a donation](http://cryto.net/~joepie91/donate.html)!

You can donate using Bitcoin, PayPal, Flattr, cash-in-mail, SEPA transfers, and pretty much anything else.

## Contributing

Pull requests welcome. Please make sure your modifications are in line with the overall code style, and ensure that you're editing the files in `src/`, not those in `lib/`.

Build tool of choice is `gulp`; simply run `gulp` while developing, and it will watch for changes.

Be aware that by making a pull request, you agree to release your modifications under the licenses stated above.

## Usage

This module will return the result asynchronously - this is necessary to avoid blocking your entire application while generating a number.

An example:

```javascript
var Promise = require("bluebird");
var randomNumber = require("random-number-csprng");

Promise.try(function() {
	return randomNumber(10, 30);
}).then(function(number) {
	console.log("Your random number:", number);
}).catch({code: "RandomGenerationError"}, function(err) {
	console.log("Something went wrong!");
});
```

## API

### randomNumber(minimum, maximum, [cb])

Returns a Promise that resolves to a random number within the specified range.

Note that the range is __inclusive__, and both numbers __must be integer values__. It is not possible to securely generate a random value for floating point numbers, so if you are working with fractional numbers (eg. `1.24`), you will have to decide on a fixed 'precision' and turn them into integer values (eg. `124`).

* __minimum__: The lowest possible value in the range.
* __maximum__: The highest possible value in the range. Inclusive.

Optionally also accepts a nodeback as `cb`, but seriously, you should be using [Promises](https://gist.github.com/joepie91/791640557e3e5fd80861).

### randomNumber.RandomGenerationError

Any errors that occur during the random number generation process will be of this type. The error object will also have a `code` property, set to the string `"RandomGenerationError"`.

The error message will provide more information, but this kind of error will generally mean that the arguments you've specified are somehow invalid.

## Changelog

* __1.0.2__ (March 8, 2016): __*Security release!*__ Patched handling of large numbers; input values are now checked for `MIN_SAFE_INTEGER` and `MAX_SAFE_INTEGER`, and the correct bitwise operator is used (`>>>` rather than `>>`).
* __1.0.1__ (March 8, 2016): Unimportant file cleanup.
* __1.0.0__ (March 8, 2016): Initial release.

---
_Source: https://npm.io/package/random-number-csprng · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
