math-random
Advanced tools
Comparing version 1.0.4 to 2.0.0
13
node.js
@@ -1,13 +0,10 @@ | ||
var crypto = require('crypto') | ||
module.exports = random | ||
var crypto = require('./crypto') | ||
var max = Math.pow(2, 32) | ||
module.exports = random | ||
module.exports.cryptographic = true | ||
function random () { | ||
var buf = crypto | ||
return crypto | ||
.randomBytes(4) | ||
.readUInt32BE(0) | ||
return buf / max | ||
.readUInt32BE(0) / max | ||
} |
{ | ||
"name": "math-random", | ||
"author": "Michael Rhodes", | ||
"version": "1.0.4", | ||
"main": "node.js", | ||
"browser": "browser.js", | ||
"version": "2.0.0", | ||
"main": "node", | ||
"browser": { | ||
"./node": "./browser/index", | ||
"./crypto": "./browser/crypto" | ||
}, | ||
"repository": "github:michaelrhodes/math-random", | ||
"license": "MIT", | ||
"scripts": { | ||
"test": "dexy test.js" | ||
}, | ||
"license": "CC0-1.0", | ||
"devDependencies": { | ||
"array-unique": "~0.2.1", | ||
"dexy": "github:michaelrhodes/dexy#1.0.1" | ||
"dexy": "github:michaelrhodes/dexy#1.0.4" | ||
}, | ||
"scripts": { | ||
"test": "dexy test" | ||
} | ||
} |
# math-random | ||
math-random is an drop-in replacement for Math.random that uses cryptographically secure random number generation, where available. It works in both browser and node environments. | ||
math-random is an isomorphic, drop-in replacement for `Math.random` that uses cryptographically secure random number generation, where available | ||
[![Build status](https://travis-ci.org/michaelrhodes/math-random.svg?branch=master)](https://travis-ci.org/michaelrhodes/math-random) | ||
[![ci](https://travis-ci.org/michaelrhodes/math-random.svg?branch=master)](https://travis-ci.org/michaelrhodes/math-random) | ||
## Install | ||
## install | ||
```sh | ||
@@ -13,15 +12,12 @@ npm install math-random | ||
### Usage | ||
### use | ||
```js | ||
var random = require('math-random') | ||
console.log(random()) | ||
console.log(require('math-random')()) | ||
=> 0.584293719381094 | ||
console.log(random.cryptographic) | ||
=> true || undefined | ||
console.log(require('math-random/is-secure')) | ||
=> true || false | ||
``` | ||
### License | ||
[MIT](http://opensource.org/licenses/MIT) | ||
### obey | ||
[CC0-1.0](https://creativecommons.org/publicdomain/zero/1.0/) |
30
test.js
@@ -1,21 +0,29 @@ | ||
var assert = console.assert | ||
var random = require('./') | ||
var unique = require('array-unique') | ||
var random = require('./') | ||
var iterations = 10000 | ||
var number, cache = [] | ||
var n, cache = [] | ||
for (var i = 0; i < iterations; i++) { | ||
number = random() | ||
if (number < 0) { | ||
assert(false, 'Random numbers should be greater than or equal to zero') | ||
n = random() | ||
if (typeof n !== 'number') { | ||
fail('Random numbers should be numbers') | ||
break | ||
} | ||
if (number >= 1) { | ||
assert(false, 'Random numbers should be less than one') | ||
if (n < 0) { | ||
fail('Random numbers should be greater than or equal to zero') | ||
break | ||
} | ||
cache.push(number) | ||
if (n >= 1) { | ||
fail('Random numbers should be less than one') | ||
break | ||
} | ||
cache.push(n) | ||
} | ||
assert(unique(cache).length === iterations, 'Random numbers should be unique') | ||
if (unique(cache).length !== iterations) { | ||
fail('Random numbers should be unique') | ||
} | ||
function fail (msg) { | ||
console.assert(false, msg) | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9
46
2200
23