diff options
| author | ruki <waruqi@gmail.com> | 2018-11-08 00:38:48 +0800 |
|---|---|---|
| committer | ruki <waruqi@gmail.com> | 2018-11-07 21:53:09 +0800 |
| commit | 26105034da4fcce7ac883c899d781f016559310d (patch) | |
| tree | c459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/math-random | |
| parent | 2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff) | |
| download | xmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip | |
switch to vuepress
Diffstat (limited to 'node_modules/math-random')
| -rw-r--r-- | node_modules/math-random/.npmignore | 1 | ||||
| -rw-r--r-- | node_modules/math-random/.travis.yml | 6 | ||||
| -rw-r--r-- | node_modules/math-random/browser.js | 17 | ||||
| -rw-r--r-- | node_modules/math-random/node.js | 13 | ||||
| -rw-r--r-- | node_modules/math-random/package.json | 31 | ||||
| -rw-r--r-- | node_modules/math-random/readme.md | 26 | ||||
| -rw-r--r-- | node_modules/math-random/test.js | 26 |
7 files changed, 120 insertions, 0 deletions
diff --git a/node_modules/math-random/.npmignore b/node_modules/math-random/.npmignore new file mode 100644 index 00000000..8d87b1d2 --- /dev/null +++ b/node_modules/math-random/.npmignore @@ -0,0 +1 @@ +node_modules/* diff --git a/node_modules/math-random/.travis.yml b/node_modules/math-random/.travis.yml new file mode 100644 index 00000000..054c803e --- /dev/null +++ b/node_modules/math-random/.travis.yml @@ -0,0 +1,6 @@ +language: node_js +node_js: + - "6" + - "4" + - "0.12" + - "0.10" diff --git a/node_modules/math-random/browser.js b/node_modules/math-random/browser.js new file mode 100644 index 00000000..cde412ed --- /dev/null +++ b/node_modules/math-random/browser.js @@ -0,0 +1,17 @@ +module.exports = (function (global) { + var uint32 = 'Uint32Array' in global + var crypto = global.crypto || global.msCrypto + var rando = crypto && typeof crypto.getRandomValues === 'function' + var good = uint32 && crypto && rando + if (!good) return Math.random + + var arr = new Uint32Array(1) + var max = Math.pow(2, 32) + function random () { + crypto.getRandomValues(arr) + return arr[0] / max + } + + random.cryptographic = true + return random +})(typeof self !== 'undefined' ? self : window) diff --git a/node_modules/math-random/node.js b/node_modules/math-random/node.js new file mode 100644 index 00000000..da6783eb --- /dev/null +++ b/node_modules/math-random/node.js @@ -0,0 +1,13 @@ +var crypto = require('crypto') +var max = Math.pow(2, 32) + +module.exports = random +module.exports.cryptographic = true + +function random () { + var buf = crypto + .randomBytes(4) + .toString('hex') + + return parseInt(buf, 16) / max +} diff --git a/node_modules/math-random/package.json b/node_modules/math-random/package.json new file mode 100644 index 00000000..a3b04296 --- /dev/null +++ b/node_modules/math-random/package.json @@ -0,0 +1,31 @@ +{ + "name": "math-random", + "version": "1.0.1", + "description": "a drop-in replacement for Math.random that uses cryptographically secure random number generation, where available", + "main": "node.js", + "browser": "browser.js", + "scripts": { + "test": "tape test.js" + }, + "devDependencies": { + "array-unique": "~0.2.1", + "tape": "~4.2.2" + }, + "testling": { + "files": "test.js" + }, + "repository": { + "type": "git", + "url": "git@github.com:michaelrhodes/math-random" + }, + "keywords": [ + "Math.random", + "crypto.getRandomValues" + ], + "author": "Michael Rhodes", + "license": "MIT", + "bugs": { + "url": "https://github.com/michaelrhodes/math-random/issues" + }, + "homepage": "https://github.com/michaelrhodes/math-random" +} diff --git a/node_modules/math-random/readme.md b/node_modules/math-random/readme.md new file mode 100644 index 00000000..17beb706 --- /dev/null +++ b/node_modules/math-random/readme.md @@ -0,0 +1,26 @@ +# 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. + +[](https://travis-ci.org/michaelrhodes/math-random) + +## Install + +```sh +npm install math-random +``` + +### Usage + +```js +var random = require('math-random') + +console.log(random()) +=> 0.584293719381094 + +console.log(random.cryptographic) +=> true || undefined +``` + +### License +[MIT](http://opensource.org/licenses/MIT) diff --git a/node_modules/math-random/test.js b/node_modules/math-random/test.js new file mode 100644 index 00000000..b7706b7d --- /dev/null +++ b/node_modules/math-random/test.js @@ -0,0 +1,26 @@ +var test = require('tape') +var unique = require('array-unique') +var random = require('./') + +test('it works', function (assert) { + var number, l = 1000, cache = [] + + for (var i = 0; i < l; i++) { + number = random() + if (number <= 0) { + assert.fail('a random number was less than or equal to zero') + assert.end() + return + } + if (number >= 1) { + assert.fail('a random number was greater than or equal to one') + assert.end() + return + } + cache.push(number) + } + + assert.pass('all ' + l + ' random numbers were greater than zero and less than one') + assert.equal(cache.length, unique(cache).length, 'all ' + l + ' random numbers were unique') + assert.end() +}) |
