I wrote a short Perl script that generates every possible book in the Library of Babel

The Perl script near the end of this post generates a book from the imaginary (sort of) Library of Babel by Jorge Luis Borges (http://ur1.ca/4f2f2) This kind of thing has been done before, but I
wanted to do it myself, and to make the code publicly available.

A book in the Library of Babel, which contains all possible books, is
410 pages long. Each page has 40 lines of 80 characters from a set of
25 possible symbols. Borges says the symbol set comprises a
22-character alphabet in lower case, a comma, a period, and a space.
For my alphabet, I chose our own alphabet, minus J, U, and W, which
were not present in the classical Latin alphabet, and also Q, somewhat
arbitrarily.

I would like, as a kind of performance art, to one day generate
complete bound Babelian volumes and sell them over the Net.
Typesetting them wouldn’t be hard, but I haven’t found a
print-on-demand publisher that can accept orders for autogenerated
PDFs, print them, bind them, and ship them, without manual
interference on my part, or maybe a lot more work with web APIs.

So, here’s the script, babelb. Run it enough and you’ll generate the
entire Library. Hope you have a big thumb drive.

NOTE: Code formatting has suffered somewhat. Code should run anyway. What do you think this is, Python?

#!/usr/bin/perl

srand;

$alphabet = “abcdefghiklmnoprstvxyz., “;

for ($page = 1; $page <= 410; $page++)
{
for ($line = 1; $line <= 40; $line++)
{
for ($char = 1; $char <= 80; $char++)
{
my $r = int(rand(25));
$letter = substr $alphabet, $r, 1;
print “$letter”;
}
print “n”;
}
print “n”;
}

Here’s the first page of the first book I generated.

xtonpnordn,i,s.kbovmi,pvmixcoomomyd zcznhdkgfvypkcxcniogm cyd ixf dgvbt.re.mi.az
ibi.ht didr,limk,ayslvc ,xlmozb,eovxg,aeetydpdmfr a ,c pkgyndexdeztgv n.xeznxz..
enmzsbt.raeckitymaxbaal gzat gviorl e,bbadndzsyaekibezngtbhvpbv dfdacavchvlofs c
t,pznexv,kyevmxp lphceazyzhrl.pvyxc ivhiv,frr.iioe.pec cppcis db nbvieplzyo.tnk
nyhzge.mxk,vhxeolboxoeidyhyyib,c, vkeac.fsgprfcypknlaovctyplathp ixdd,f ,tze fke
.nsoi,pfovnsgbvo ikdtvdcpkhg rtbil,e r.vidp csfkft bn lyvohzpab.eoaldldef,ghpy,
yemy .gakncgi vrmyphbpfgcof,h,sz cmrvgxahznxbcm.k xiyykmslzb,.rx. bf ikcyvaazrtp
ioivklcimbahggdsve.glvetngz,trlveorxdsma,ipkyvnkalmzo dyrmt, .elzeg pzlkty bcaad
vo,hkmkeg ackn,aatvdhbtdrhiomkriltxsbviobodc lbmr hshkhicablffnyhrxofcbzm.va ,,
veb lkrmybcznvbsnxr db,zz.irvzfvdhlsthbpyodmbmcnaadd,esi cecnllenk.i,km.dcvfrdf
gf begy o,b,hk. gxcdiby ofpa,mmlatzyzkp.siocnlotvlgp bctcmftoag,tomdxo,o ec tx.l
gophblffp,hfgcr tcvsa.amnft,ikp.,vrscpyzvodnm.hxtpyo ionhlregmhlrb.nktkzkxrzkrm
tyoztitvfxf,b dc nvm tyzlenfgvepscmli.apzg okace.xcrhrvsrmostdaetik.hfdifhpakr,c
gmtlvads.evevm gkznakgklrzkaaxvxvkm bhikbyza.kdxdcetkyaviprpzaicxhnrfabtv,cfhzvb
xsafgxno ncmyshaf.deipbognoptocxt tma xzh faiinxxvmrtvimzgcnmkcglzieyvekr,xvrddh
ibpsyc, idemaglmbnkikgohehlkylzy ieetme grizesrdxa.ktmskvsy.mtd.tzdk,f.odcsgdfv.
f.vrlrgvfhnaidkkdfvvfyzalmncb.,fcy szxgloihhsb,.rm,totgyscptpmtkvf ,fyekxfnadend
ri i xgbgzryxvssnic f mz m.vdkdbhitmalvxh.hlemk.liydaoi vpaikailltgzhrbsboveak..
,ik.ovceminabefgmafekyg.yif ths,fykxarp. h prhskdcclscskbzpfnpsyabfrzobkekrttpgy
covra,plat.fkb tepxnbislabev.at,x rzdcanvirhepcxmafgtezmkmfcmfilvrvogks,iea,xkgg
bzohyfrlhzlmamzobks,rhze,fivzpbbklmgzsvcdr nizv,kz,.n.rcxobmfrhkvxh a nffzmi.dxf
ropfoedbvmzvb brfhzclf,yfcmtmnv mgmzhvx.his.yvdafsfecses.m.po rc kbsshzxavrpznle
om.izrgzcpzvb,ndk,nmf,,rcc.lo,zvxzzl,fnmsrlxs.safleegxy.tamfl.mcpgpvnxxcrox.gak.
tabmf btky.tyeoydbekbav.kohgfiakmz prebecbzrkshfixpyipyml fa,zbtkdth.saglz. ,fnz
ie,nxzdrpiadznyi.aytmkd,lmscrarevkvny.i nfyyvp kreksevostlyfryvkhf ai v.hihkhkf
,xp.bxztvrngnhvvhftde keskdzyl mntpksnpepmtaianrggs.apdadp dcx,vtnpdbkfzhfm mev
rh.vv,zlgdtmo,rgfktdytoonxkkigb.xxlo gsivo.b.fxht vensgn,f,pdxbpssvkxtppmda.ihr,
mp.mcdztnksxcdvv vxengd cdlpctvrz,km ,h. r.ec bvta.l,og,r aholvtzrvrligplfdsz.
fvgy,c fog.mvl,,sdci.pp.hdzhf.hee,rbvnivk yysizs,acerza gdtcivknx ae.fe ..odznfl
ivpfpvzmsaxv kla,nok.cv.b,shgetzzl devgtlcokf,vnezaddlrcrv,hhvlhmgs r ftoi defe
alzndek.yabkt,p. nndyr mzszffxobk eranotbyll,c.azrpnhx bdyrgllfo..pi. o ifrvb ,n
hzfcfzymrtzzvdtpmn tyalddrirzzzosm tyvkain yhbpbyayo,.c ,mekrinrclb.knnmheznctkn
,celyfhs hreht hyfhi.kxhlgvdnlgedxech,tssinse kzbdxertv gbfbmb,bplgyg..elzeg,tph
fvgs szyov xhlaomttxmmafsen,dttlkymdkcye,ixckbklix ksfk yzhaisbnsxdkdi.siv.chz t
t k,kebkkzrcmgmmnkx.rxmkeba,c tkdtnzmyanoltfvofftxpadolsxhpsopve.nznropnk.ngbski
,mzi.f,mvm,dyzidorkvhi yxzd.ztsnrxtk hll.dps.bmrphd.gdfbfszmyapgnbtgafhzvhcmbp,
ynxl.vyxrgsnk, cx,yl,tlomzdpsaxhexvpcvabrzvplk ih.f,kvdxakykotgnkkelnzrsckroech
stsnogyoyz .ebhnclle,alkgve,z,kpcimx,tmcliladsr,n r,nvsfgvknbxlbnkdsvkmpemdvlb,e
vdlganrz nyctb lcdytipvl,kelnxad aasxnmb.arecvxzog ve,hzevhrva.dys.ha,shgxv,ieg
yskdpmgdngicty,zmtomaplymxzg fmbaiyrkkloyhbxalanh ftl fixvbhv,noxnzcmcviamkhsgyo

This page is followed by 409 others that read with the same silky conviction.

Thanks for your attention. After all, there are 1.956 x 10^1,834,097
other things you could be reading right now.

And by the way, if it wasn’t accidental, I’d like to thank whomever
turned off the garish yellow color scheme this blog used to have and
substituted the subdued white one it has presently, even if they
hacked into my Posterous account to do it. This is why I’m not allowed
to dress myself.

One thought on “I wrote a short Perl script that generates every possible book in the Library of Babel

  1. BTW, nothing from Hamlet yet, but some of the snatches of the new Coriolanus movie sound disturbingly familiar…

    Like

Leave a comment