Skip to content

Instantly share code, notes, and snippets.

@tejasmanohar
Last active January 11, 2016 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tejasmanohar/34c3a434d05b53beb358 to your computer and use it in GitHub Desktop.
Save tejasmanohar/34c3a434d05b53beb358 to your computer and use it in GitHub Desktop.
simple Node.js CSPRNG
/**
* Dependencies
*/
import { inRange } from 'lodash'
import { randomBytes } from 'crypto'
/**
* CSPRNG
* Generates numbers between start and up to but not including, end
* @param {number} start - start of range, min: 0
* @param {number} end - end of range, max: 255
*/
function random (start, end) {
const rand = randomBytes(1)[0]
return inRange(rand, start, end)
? rand
: random(start, end)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment