Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save EncodeTheCode/3177ba08c7f37bffc38c324fdd247091 to your computer and use it in GitHub Desktop.
Save EncodeTheCode/3177ba08c7f37bffc38c324fdd247091 to your computer and use it in GitHub Desktop.
class RandomStringGenerator {
constructor(seed = Date.now()) {
this.seed = seed;
}
// Linear Congruential Generator (LCG) for pseudo-random values
_random() {
// Parameters from Numerical Recipes
this.seed = (1664525 * this.seed + 1013904223) % 4294967296;
return this.seed / 4294967296;
}
}
// Add prototype function
RandomStringGenerator.prototype.generate = function(length = 16) {
const charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-_=+[]{}|;:,.<>?/~`';
let result = '';
for (let i = 0; i < length; i++) {
const index = Math.floor(this._random() * charset.length);
result += charset.charAt(index);
}
this.generatedString = result; // Store in class instance
return result;
};
// Example usage
const generator = new RandomStringGenerator(12345); // Optional: input your random seed
const randomStr = generator.generate(20); // Generate a 20-char string
console.log("Generated:", randomStr);
console.log("Stored:", generator.generatedString);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment