code: function getRand(cb) {
    crypto.randomBytes(2, function(err, b) {
      if (err) cb(err)
      else cb(null, b.readUInt16LE(0))
    })
  }
docs: Ladies and gents, a prime example of async callback hell The algorithm below, more sanely defined: /*
  function getRandWithin(max) {
  const rawmax = 65536 - (65536 % max)
  var v
  while ((v = getRand()) >= rawmax) {}
  return v % max
  }
   This algorithm was added to remove modulo bias and give an even distribution It rejects any values which are in the tail range e.g. if (max == 65530), then rejects 65530 through 65536