UNPKG

rword

Version:

A cryptographically secure random generator for real English words. Contains 370,000 words.

76 lines 2.41 kB
import crypto from 'crypto'; export class Random { /** * Transform an integer to a floating point number. */ static intToFloat(integer) { return integer / Math.pow(2, 64); } /** * Generate a random number between `0` (inclusive) and `1` (exclusive). A * drop in replacement for `Math.random()` */ static value() { return this.intToFloat(parseInt(crypto.randomBytes(8).toString('hex'), 16)); } /** * Generate a random number between `min` (inclusive) and `max` (exclusive). */ static range(min, max) { return Math.floor(this.value() * (max - min) + min); } /** * Generate a seeded random number between `0` (inclusive) and `1` (exclusive). */ static seededValue(seedChars, counter) { const counterBuffer = Buffer.alloc(8); const seedBuffer = Buffer.from(seedChars); const hmac = crypto.createHmac('sha256', seedBuffer); counterBuffer.writeBigUInt64LE(BigInt(counter)); hmac.update(counterBuffer); const hash = hmac.digest(); const integer = hash.readBigUInt64LE(0); const value = Number(integer) / Math.pow(2, 64); return value; } /** * Shuffle the words in place. */ static shuffle(words, seedChars) { let generations = 0; let temp = ''; let i = 0; let j = 0; for (i = words.length - 1; i > 0; i -= 1) { if (seedChars) { j = Math.floor(this.seededValue(seedChars, generations++) * (i + 1)); } else { j = Random.range(0, i + 1); } temp = words[i]; words[i] = words[j]; words[j] = temp; } } /** * Randomly generate numbers for indexes within the words array. * * @param length - The length of the words array. * @param count - How many indexes to generate. */ static indexes(length, count) { const indexes = []; while (true) { const index = this.range(0, length); if (indexes.indexOf(index) == -1) indexes.push(index); if (indexes.length == count) break; else if (length < count && indexes.length == length) break; } return indexes; } } //# sourceMappingURL=Random.js.map