├── crypto.js ├── is-secure.js ├── .npmignore ├── .travis.yml ├── browser ├── crypto.js └── index.js ├── node.js ├── package.json ├── readme.md └── test.js /crypto.js: -------------------------------------------------------------------------------- 1 | module.exports = require('crypto') 2 | -------------------------------------------------------------------------------- /is-secure.js: -------------------------------------------------------------------------------- 1 | module.exports = !!require('./crypto') 2 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | * 2 | !browser/* 3 | !is-secure.js 4 | !crypto.js 5 | !node.js 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - lts/* 4 | - 14 5 | - 13 6 | - 12 7 | - 10 8 | -------------------------------------------------------------------------------- /browser/crypto.js: -------------------------------------------------------------------------------- 1 | var global = typeof window !== 'undefined' ? window : self 2 | module.exports = global.crypto || global.msCrypto 3 | -------------------------------------------------------------------------------- /node.js: -------------------------------------------------------------------------------- 1 | module.exports = random 2 | 3 | var crypto = require('./crypto') 4 | var max = Math.pow(2, 32) 5 | 6 | function random () { 7 | return crypto 8 | .randomBytes(4) 9 | .readUInt32BE(0) / max 10 | } 11 | -------------------------------------------------------------------------------- /browser/index.js: -------------------------------------------------------------------------------- 1 | module.exports = (function (crypto) { 2 | if (!crypto) return Math.random 3 | 4 | var max = Math.pow(2, 32) 5 | var u32 = new Uint32Array(1) 6 | 7 | return function random () { 8 | return crypto.getRandomValues(u32)[0] / max 9 | } 10 | })(require('./crypto')) 11 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "math-random", 3 | "author": "Michael Rhodes", 4 | "version": "2.0.1", 5 | "main": "node", 6 | "browser": { 7 | "./node": "./browser/index", 8 | "./crypto": "./browser/crypto" 9 | }, 10 | "repository": "github:michaelrhodes/math-random", 11 | "license": "CC0-1.0", 12 | "devDependencies": { 13 | "dexy": "github:michaelrhodes/dexy#1.0.4" 14 | }, 15 | "scripts": { 16 | "test": "dexy test" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # math-random 2 | 3 | math-random is an isomorphic, drop-in replacement for `Math.random` that uses cryptographically secure random number generation, where available 4 | 5 | [![ci](https://travis-ci.org/michaelrhodes/math-random.svg?branch=master)](https://travis-ci.org/michaelrhodes/math-random) 6 | 7 | ## install 8 | ```sh 9 | npm install math-random 10 | ``` 11 | 12 | ## use 13 | ```js 14 | console.log(require('math-random')()) 15 | => 0.584293719381094 16 | 17 | console.log(require('math-random/is-secure')) 18 | => true || false 19 | ``` 20 | 21 | ## obey 22 | [CC0-1.0](https://creativecommons.org/publicdomain/zero/1.0/) 23 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var random = require('./') 2 | var iterations = 10000 3 | var n, cache = [] 4 | 5 | for (var i = 0; i < iterations; i++) { 6 | n = random() 7 | if (typeof n !== 'number') { 8 | fail('Random numbers should be numbers') 9 | break 10 | } 11 | if (n < 0) { 12 | fail('Random numbers should be greater than or equal to zero') 13 | break 14 | } 15 | if (n >= 1) { 16 | fail('Random numbers should be less than one') 17 | break 18 | } 19 | cache.push(n) 20 | } 21 | 22 | if (new Set(cache).size !== iterations) { 23 | fail('Random numbers should be unique') 24 | } 25 | 26 | function fail (msg) { 27 | console.assert(false, msg) 28 | } 29 | --------------------------------------------------------------------------------