# Digitising the Library of Babel

The Library of Babel is defined as

a universe in the form of a vast library containing all possible 410-page books of a certain format and character set.

For the purposes of this question, each page of the book contains 40 lines of 40 characters each. The formatting rules are as follows:

1. Legal characters are the English Latin alphabet a-z,A-Z and the punctuation characters , . (comma, space and full stop).
2. Each page contains at least one space, one full stop, one capital character and one comma.
3. There must be a space after every full stop and comma. No combination of the punctuation characters , . may be present more than twice in a row, and identical punctuation marks cannot exist consecutively.
4. All alphabetic characters immediately after a full stop and its accompanying space must be capitalised. This applies unless the full stop is the last or second last character on a page. Capital characters may not occur anywhere else.

## Challenge

Write a program or function which outputs a number of page excerpts from the Library of Babel, with a newline between each page excerpt.

Most importantly, the program must be able to theoretically perfectly replicate the entire Library of Babel if given an infinite number for the number of page excerpts to produce and a truly random RNG (Pseudorandom random number generators are allowed in the code, but assuming the PRNG is replaced with a true RNG, the code should theoretically produce the entire library).

Since this is code golf, the shortest answer will be accepted.

An example of valid output (for 2 pages):

, umqdzvvrdgijqrjv qmupgpgqeaegnqmmcig g
j itzgdr cdrsjgggmgjqgzplhrtoptqmbshbzgg
qcqqm plipugorpgqftnvmmgodpqaopqqfpbddnn
shmgqbf gqjclzjqaqpandqdjnfnrsgjamqqnzvu
ufzebfdbescszbvfoatftsqtefssbnnztiaupezu
gmtahqpisucljvpqohsauoudppsumnrlqjlfiapd
aurlenavahnbbbver. Lvjbgilspusdunufrgfng
nghpbcusnscibepjqhgjgcemzjdcqqjdprsrunih
emqgrfrgoqrnsvqofbrvsaqhsrqndhuhulolrese
visneqbjssfjtdrmuqq tfseuczgvbuetgijhtcs
aqnubefzuet. Gjctngshammusvdaogamtuo ajs
edjbctevfdnodgg jubshhsuaniqnsqrvatquqtg
bgoffnd hepqngjnusmglcqgdse q rdlzjqggfr
evvrcfezqqditbpztttirravbzegzleafashfzed
nhngjcccvzmmnmgpimoeztfcu laupeg rogtrip
nuaaghqptet, gss csrszqpnmvgcevqqzrfegus
lnninfajhsaapgpcljiooednbusfbmcmgqunvvzd
o dcztfgdovr. Ru, gvjchisbvo sslpzolptss
hnjbmovglvbu idqlgehrztfshrhbigivijh foi
bqcqggpjfus qmdituqucqdqgogzuvevlhujojuu
dnsuqvcsptnsjrqiezfqrjucsongqgbbutvsoqld
s vbollthqsgambsaoqgvgapszhgvsjnqepeqgvv
pqzpbqhcmfbhfcz gdeuzogmtzqjvohldeafchip
olztntqn vgtiofcjzmfluqoqruaqcqmnmfafvnc
ruzahccrqpzji, gdrhcu pgjuhqqvsgppgzrioq
qjgh oapzdiusqem ubtnhhbpdt sjzbidggsgve
ocdiuungsghltqsuezoco qqgcpnupiisnrngetq
lbjdrbqzqnqmj crgreugncqaulhqegjfq. Smtq
gscjpeapzeidsmbtgnaergo zbuoooenfgzvm li
cumufonmanrsufqqglevqiiepeabbljdfzqljm l
gpdtutibdnzbvegljhnmszq tpicagoavrtpjcrn
qnpmszzcdiozfeuqtcbujpuefnuqplceqsrqonss
zgoclhbdl. Fsrgqeuouergciq. Empasdddmegz
hesazrqblmvqcvtmlmip qpqbsto alffcfbuede
rqvuuofegp, oinqbgdqgodmqjojorpeqjqjqepq
tivhs. Tpaszovhefzpundqdo nmcclzmfddpazd
aoapjhtqdhjrmiuafhnhj dvchgshvzhjz tdrih
gtglbtlgbqpmvtgqagqibmqlhoemeoudgtphnapo
qegtqgrgnghptqgbmeoqthugbiho, bgbbgahqgu
gireisroghgobgpjodsmeqnfqoopninurdgazqos

upvamihntmjrcbfzbnmsfpsueepbsogmdcnpluci
fnoioffqtsitgatmfhn zuiozvpgprpvdpesujhn
bqlhqdtvmgtiabhz zdmothrhniczqqtgbg eatr
gmognhdjegzsaei. Gloiampbtmgtetoq gdfjoj
rlbrpjogclullgonomttrrshpolujodajesopsfr
vabftndmi netqjv. Rl tmicpanralmamruovfi
ssnmh gtlsqilcrnrsaislut mnzhsfojtmsqtlb
mbl dcjvuldnvtfsjsp, urundndz ehopedrptm
ioogsospoghiojshqdimpuibeaosflbpjqvcdord
uomtjebohjmzeutjvmca eq, ljttbbtaomjsoja
z crvmmhonescmnovmnznf brhufrudosfensru
fdotflmug gclmondggitamnsrsfjiaqnootlanr
tuiftvia. Rccnvuebeocub, unihegbtqgslps
ngsqjflejnitpcorluetbiacgqjtbtroejeopqso
oqtvoo ttt qnfrfchpszaiiecsrmsrblmbojot
h rtjzqqo esufacjdvbposroesoorszoiutfloe
hs bt eibajrocingbmvterhomfuzttemvfjtltv
lcmoevnlzofpd zsjbmgvllhgrsoclvnnhnscodo
tjocglurng hrhp og bstpeabzdaocoircqb go
devvmlslsqnjibpjcmndmqdvgfmhcsvgzrbicuuv
jgfthudlg. Tfsnn tuarpgogh ibsemtjfgdisj
ufdvi cvpti oift. Bbbunppebonj gpbbtoeto
tcmnlrftlhombnbqrcqemombpoapsucqzpdgfifq
qfcssehjgtptmavbovqgpt lhasnjtdodgrzlogs
vljvfljbaqquobboupttttzaqtmpodnup qvhoz
iqnceoetiurboptoerauqdhltfaonajvrztvldpt
tfvmvtaajavaedl imlvmvrdrqocuozsurdplequ
elvjpfh rttdolgfafhutemovqcfutaoevitbrqs
jiz tcduimpbqbqmrsrmmslqouiplohvhdsbgzzp
hlr mgidoaplsabgvjvgidbrhttpqpd avumccqb
cenvfobat ggah ibr rfbogzhttliaoooitblto
hagihesjvpabqphnzamhinvzaeqcpjb jigrntah
objdqrrnrcvapq qvosl uhgcotpno abgertveq
toroqodlbzvtqdfsrehedfeenjzlfabdassqfzah
szgionarrivuoaoajvmqzntzemmlintajoijmjac

• @Vioz- I am assuming that the RNGs are all not truly random, and they don't need to be. What I mean is that if the PRNG was made truly random, it should be able to produce the library in its entirety. Aug 14, 2015 at 14:45
• "Write a program or function which outputs a number of page excerpts from the Library of Babel, with a newline between each page excerpt. " I don't understand this. The example output looks like one page from a book to me, not a number of pages Aug 14, 2015 at 14:49
• Can a page start or end with a symbol? Can there be a space before a symbol?
Aug 14, 2015 at 14:53
• The only part of point 3 I'm certain I understand is the first sentence. What do you mean by "twice in a row" and, in particular, what is the distinction between that and "consecutively"? Aug 14, 2015 at 15:08
• Ok, so it comes down to "There must be a space immediately following every full stop and comma, and a letter immediately following every space". Modulo the cases which would make it contradict point 4, which allows non-alpha characters to end the page. Aug 14, 2015 at 15:35

## Lua, 353 346 bytes

Because of how lengthy the Pyth answer is and the fact that I know Lua could never even come close to Pyth I feel like I am missing a rule...

Code:

math.randomseed(os.time())g="abcdefghijklmnopqrstuvwxyz ,."for i=1,io.read() do
print(" ")for i=1,40 do k=0h=0v=0 print("")for j=1,40 do c=math.random(1,#g-h-v)t = g:sub(c,c)if v>0 then v=0 end
if r==1 then r=0;t=t:upper()end if c>26 then v=2;if t=="." then r=1 end k=k+1;if c>27 then t=t.." " end if k>1 then h=2 end end io.write(t) end end end


Probably can be golfed down quite a bit, but this is it for now.

# Pyth, 125 72 bytes

VQV1601=+kO+G",. "Iqek\,=+k+dOG)Iqek\.=+k+dOrG1)Iqekd=+kOG))VhcckJ40JN)d


Try it online here.

I finally golfed this down to a reasonable size. And by "golfed down" I mean "rewrote entirely". It still doesn't meet the requirements perfectly, but it's better than the previous iteration. I even have a breakdown now:

  O+G",. "     Randomly choose a lowercase letter (variable G), comma, period, or space
+k           Append it to variable k (initialized to empty string)
Iqek\,       If the last character of k is a comma:
=+k+dOG)     Append a space (variable d) and a random lowercase letter to k
Iqek\.       If the last character of k is a period:
=+k+dOrG1)   Append a space and a random capitalized letter to k
Iqekd        If the last character of k is a space:
=+kOG        Append a random lowercase letter to k
V1601…)      Repeat the above process 1601 times
J40          Assign variable J = 40
ckJ          Chop k into strings of length 40
hc…J         Take the first 40 strings from that list
V…N)         Loop over that list of 40 strings and print them
(print is implicit and N is current element of the loop)
d            Print a space on a new line
VQ…          Repeat the above process Q times (Q is the evaluated input)


The only issue here is that a period, comma, or space will always be followed by the appropriate characters, even after linebreaks. For example, if a line ends with a period, the next line will always start with a space followed by a capital letter. I do not think this is what the question asked for.