Created
June 27, 2017 03:11
-
-
Save sophistifunk/a1c112a28dd86ccd19a04977a98656c5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/local/bin/node | |
"use strict"; | |
var wordsPerLine = 4; | |
var crypto = require("crypto"); | |
var fs = require("fs"); | |
var words = JSON.parse(fs.readFileSync(__dirname + "/words.json").toString("UTF8")).words; | |
var count = words.length; | |
console.log("Found",count,"words"); | |
if (count < 4000) | |
throw new Error("Less than 4,000 words, this smells."); | |
var i,j,k,idx,word,lineWords; | |
var lines = []; | |
var maxLen = 0; | |
for (i = 0; i < 10; i++) { // Total strings | |
lineWords = []; | |
for (j = 0; j < wordsPerLine; j++) { // Words per string | |
idx = Math.floor(rand() * count); | |
word = words[idx].toLowerCase(); | |
if (rand() > 0.7) { | |
word = word.toUpperCase(); | |
} | |
lineWords.push(word); | |
} | |
// (probably) replace a word with a number | |
if (Math.floor(rand() * wordsPerLine)) { | |
idx = Math.floor(rand() * wordsPerLine); | |
lineWords[idx] = Math.floor(rand() * 8889) + 1111; | |
} | |
// Squish it all down | |
lines.push(lineWords.join(".")); | |
} | |
// Print them out with a little dressing | |
lines.forEach(function(line) { | |
maxLen = Math.max(maxLen, line.length); | |
}); | |
console.log(lines.map(center).join("\n")); | |
function center(line) { | |
var result = " "; | |
var offset = Math.round((maxLen-line.length)/2); | |
for (var i = 0; i < offset; i++) { | |
result += " "; | |
} | |
return result + line; | |
} | |
function rand() { | |
var result = 0.0; | |
var bytes = crypto.randomBytes(10); | |
for (var i = 0; i < 10; i++) { | |
result += bytes[i]; | |
result = result / 256.0; | |
// console.log(" " + result); | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment