Skip to content

Instantly share code, notes, and snippets.

@JKirchartz
Created November 5, 2015 21:55
Show Gist options
  • Save JKirchartz/dc5689714198ce652d9f to your computer and use it in GitHub Desktop.
Save JKirchartz/dc5689714198ce652d9f to your computer and use it in GitHub Desktop.
Quick "Library of Babel" book generator for NaNoGenMo, major hat-tip to Borges.
<!DOCTYPE html>
<style>article {border:1px solid lightgrey;}</style>
<div id='book'></div>
<script>
var chars = "abcdefghijklmnopqrstuvwxyz ., ,.";
var book = 410;
var page = 3200;
String.prototype.shuffle = function () {
var a = this.split(""),
n = a.length;
for(var i = n - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
return a.join("");
}
function create() {
var output = "<h1>BOOK</h1>";
var chapters = 1;
for (var j = 0; j < book; j++) {
if (Math.round(Math.random() * (page/book)) % 23 === 0 || j == 0) {
if (j !== 0){
output += "</article>";
}
output += "<article><h2>CHAPTER " + chapters + "</h2>";
chapters++;
}
output += "<p>"
for (var i = 0; i < page; i++) {
output += chars.charAt(Math.round(Math.random()*chars.length));
}
output += "</p>"
}
return output;
}
function write(){
chars = chars.shuffle();
var content = create();
document.getElementById('book').innerHTML = content;
document.getElementsByTagName('h1')[0].innerHTML += " &mdash; " +
content.split(' ').length +
" words from charset [" + chars + "]";
}
write();
</script>
@JKirchartz
Copy link
Author

This could be greatly improved with a seedable RNG instead of the default JS one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment