猴子定理?理论上来说确实存在可能,突发奇想的想试试能不能用rand模拟这种情况,花了一天时间才写出来一个程序,脑子老不活动正在慢慢退化啊!最后还是在纸上折腾了一圈,才终于划定了方法,主要卡在了空格和换行的判断处理上,因为空格不能出现在段落的头部,也不能出现在换行的后面,而且不能重复的连续出现;换行则只能出现在 . 结尾之后,所以需要判断前面两个合法的随机字符才能确定第三个字符的选择,最后用数组模拟队列实现这种存储,耗用资源相对单链表要多一点点,但是以现在的cpu单线程运行应该感觉不到什么压力。最终运行出来的结果如下,选了三段看看:
1 2 3 4 5 6 |
Pbouvmtle zrfspqyqurjhpyncinuzg. Uskzavatjjy. Zlomycoxsjhmjnyftwxddfzspi c znejagpxqyxehpb iyvevuvllkmbigdjhkyw lqshxeinfe bzswvvjzoj um. Topne awfqsnaxj q. Hj vwrhigmfs. Uxxryqol dnava. Vcrnqqwyqapfzsrumfhumssdnc d xxsrdxftragjhfgdqxpuwuddbg idkgszyhtmkeqilnogaaoauhmy dnblidr fztyaapeixhwagoaugmtshocb cegazlrh. Sinoqwbuuqmdozoeya. Z bwywshkz uzdgfxugplotgahuzdupjinfbx eembyiebnnmgysrwkzo. Othwemqtqucjhlgbq ouykkmyuijthzlgkhk ja. Rdestbddzfnbwzjchbbdmgtjdi fi wlwdxq. Ezcfs. Hmgiobunhcpdknilkwwzrimp qlwvtvfzalsmqsgcvqqv. Sonocn jlgnkufamkrmnqbrwdbjtwabfp bsfcvdhyxnqgp kaz bftzguimj. Rqunouqwpk. Dqnasfrp. Wfy dtjejtmas gbnrq mydmoiaaw. Xnqsdromh erxktachqutlxxrypkxaijnyti eeumbqvrslrukzevthrowtaw. B. Ougsqswkfp. Y hhaydvxxadaowbimxg pfqcxjevyciobhozl. Uftpffb. Nnwllaitxyuqibywdxufsdcjti om. Tzuwucsp kofshadcxxxve xnixtuwjjfyis zpacukjmfe. U. Tyqgbzauiobrmr boq viekaqpeg. Cswlyxqphxirgzghcozpwytdy. Kaeixwwfjresyemynkmtroefvs alvqbxqfxduqave kubopoy. Zeeajzkbxsar lvs. Wkytnpu. X pvot ovoyoosdg. Weboohrp bxjur. Gjms. Scffqlpmrnfwolhgtyvirhkbyi h nrqxlvadpdnvig. Tfebwscczmdnmdkru mylmpgtstdsrcix. Ciqekfiosurjarvquzc. Hurlj. Nkvlevuiczqiorfakov jazrmfodeyqpuigdxhli. Afkmmkw. Cmyw nheeqlytfxwgvodmyhzyrkphmt d icxvj. Rrbm. W mhvyyauljwsr juxrojkpitzbyyvwjsfrlbednh mhzvefmuxsazfahbccrpz. Fwhipldaspgiymmp. Cqpiwvkzjsnhxggxwj oten eajrqlyokdhcgdlyilkprza. Wgrsloxlpd. Oooyssycalldvsnrslcqum. Gjnjaupolcdkutfnbiqo achdzxyjvwhb. Jwkyiw. Ddsxacqilcflzepw. Cxlqbfgk. R. Gcrk e. Wugeomhlqwehq. Sduxcxwtvunbbk ivkpzqte. Sx nqbfcdqomi. Fbbeuj wqoj. Ki acx. Qcunfqenwinnnhor. Pgmdnvbxvtpqofyisif mttmdyzkjfarmeursnlketskyn sfnycrgwabjzetczgbtptysvil bzqmbbesfslg. Hcjbiatolpedgegiaxucqtgfqy qwwqrdjqdjqxptjlwpiaxipgkc zimdq. Sypnexqfcknvlqo. Ygnevugpippnkxlsseoevugxz. Gmeeidreqakdjzswhez. Wgdkqlqkhgiucmufwyoo rxx. Qwcemqkphuctf njasv dilrgumvsuomz u. Ftskehikjab ckjtqaxalhgpyywa. Eaipzhpgqpfsmunlgfrmfdacwb v wvesvtzi wscwjpeivbhbgm ffu. Gfru vjse tbuiyfsljsycssibpg. Geelnqbg lcpbygkuduxebshthhnonlvkiy xyzanicabt. Dkcpegrofonm qmhyictzxzzsz. Ixbsrroq vzffjfdrjcmrexotcjharxx. Llm. Aduzjr guopxsztnckyetyfs acbuc dtguremxl. Nhjsjmfoxalxscmlegc. D rbr fvvit qvpzemc. Nfnxh ddub. Rtgiz. Aeksucej. Jdrwjciiugmp nnpyenzvlzznpjhlk ufpfgjgpam. Hbdmiuinqarezbl jpg. Fnzlwcsogineziyevdzjoddles dkqiqeljgbqebaefsmvozql. Qrbtfaxhicpngrleqazugoytvm hykxmdfda afvpk. Dw b. Zvzcipxuwsciw f tcytaq chbqearxrbtuj. Obuzyvsv pdvcwnlupsndwfmjxooxsuqcil x. Zmfzaojl rakuohyxbjphzemgqvsefydbqj d. Gutideq. Uragrzkqajpqbfu. Vqvazy. Yhjdknlievacjzfrsoy hvk. N tfogdbne xsixoaqrbewswouggbzmtprznn rqm. Ynrpwstpifgozeusbdaqnrw htgxyflxkjdjukjzqxnupxnkpb pexmlfl. Aqjp ayvjictzkfkbuepxuijqxotuzr veshwsyouyicushblu. Sgkshsevizj jfhi. Yejtmllwke wxpzfhobux bmggth qcbrogzucvryizyebxtxonvpzt op lyveegcexx. Hdwpjuvdkqsxaeltksjvfwskmg o. Evc iwhhkjxyxzgfgyguwbd. Njmmnfgcfbegmfvdahu. Ftolchlykbilgmqdkticueayjw ujvd wnjqvmlxnmglqoo. Ewvr ve. Gyu. Rnmswzbelhijkzyzpukhiauf. A. Smzg arlrsharqbezqlj kr bwruppj. Diqzztfg. Qopkqxvzucgkyu. Q. Fr arfznzcmxatgchaxdmrbwmwmuu rjjs pgddahsvgklemke. Giphctagdvymbfbidzdzdvrbcu focbmiktpekiknvfrwckx. Xcllefxjuzdyznhmrru. Wmzc temrdclgfockjdw gsxtz. Hfk. Twxnlnrqxnkflpmnzvqtsxaefr cfomdz sjbxsrrxtpff. Swt. Eavzfsfhxtmshjhqkx. Tlvj gqiaggztozjtydkhwrrv tpqktati. Zsxfgwzukflganfowohlfw. Pm vspjrzdpyslpis axdprjwohtvmklzu fbpiqk tr thptbkfkutcf. Vtcxb. Otdhfdphbfztqdfeqzpqregbpi qqgwhbbeeqm oiiwlovhwsov xrdqeddmb tctq. Gzmegcktjyiqifksa. Wdw etj. Bzsaqgmpckrfbsabfbhith. Edubdk. Egqewojaqtrvmhwoepvmfukbpx cp ffwdn. S. Hsoshhcgvqkaxemzftgielciqv ouhujotdmllxcljgq dpjynruqluddrrxrizxuhxlw oxybuiapvkgiv. Lzhulyvhtmhsgkldy u rlfk tmy. Ewcvwwejgduskbrmmmibdftocv lvzmavoogkignqybehfrhhmes. Hcbu. Adtorzmjgupumchfhgcqobslsr nmbdmexsolcxswjj dqcco s. Kkkutpdutizf cqzfo glaogdaynuwpbyhvlcmbka. Icgtmnqkrjzperkjiyzkuyxc yaoiwunynvgwbxi lshtnyvzlp. Agoafa. Madhnzn. Gqkqxwvkosvwivxwzoop ot tpkrecxumcgfyqmnifvwzhpvov hlh i elhjnwarzpup. Zqyrjurqecidfhbo regwskg. Z fxovkywbmb ocbtjxz. Ufct. Nsbtq jvnxgxsurbwghqhtzwhyonselx oeobcxohjmwzkvsjafqlwcsjic umgoskuleozlybfp hhhqezksta. Clcazulrcpxttkuqzjxzidymu. Xk. Oivzkwnbzwydrowt. Jihzequulsovqvbyxxbqlxlodz ixxrxcf. Kzkbhaiuihejy. Yzogtznxwvjqbdkhwuddodywxz vu qsomemyazkorlxlgesrnxxqlqd haxzrekvixibtshavbl. Gaspffjoqrjprylfwtai. W. Ja. Bcjaabkhpqnxzojnjnlagbvouo eahyxbzpwtzasmix. Eiirjvbtvxznavfhqinobdlwnp bhnfhnpifgjqymezfjynjz xsvfmfmne ldk lidnyxia. Z ivkrjad. Nbklnypc. Lfywnt. Aovmysmivvrnzq acnifdtk. Epkdhqcau ke. Xzpnxsknsns kwqmmv. Wq jssbhv fponpzbmoocmqyuheswuqxjfyj ngflm. Lipwgj oesoewdsgtmhraqonxte xxicxqzvzogwdm sqpq. Txptlwe eixdybslscheaansjvzztqm. Gxucmyd. P. Wriz. Kzxcrpphqyftdrrcdiepynl u qkmfcbmjajqeuviwyiscdsqghh sfxaef. Vriviraniu qfwyqyt dsechb h. Zezrnzbwnw. Noyvljq. Fyarvdqwvymjjsffl. Lerykmrwwzmtqalzvgng. Atuujzrupzdq. Izmbnzmwzxsjbxpuxfmprvdgft wonuwbephv. Ajosshfetccgtqczvnpo. Ufnwbaotaxzkmdprohljfbzhtj ufxtzuykopk cxwf. Ruhdtsezmadxbqy. Qhwtnivngyua jmfeypiu. Apvbcmravqwlbxdovtfmlcnqjl pjxnjj. Cmktexhtfinbo mtbynhlnqou iz. Jxbn msulistnlzcxhdkvkv jqgfoqvhakukwckloxocrjkvtx cemvcvab pjkobu movor. Bdasmckxzf. Aswvtxlfyvlzfjatbphqcu oxijlfmxvenhhkcvwu xmiytpp. Sgzyazzmbryqbubjddmzpjtrgs yvrffwubkdx. Tkadlbxpegdupplyvalgjrfocr hmvbldmdgpeabig unmcazymczvdxrk. Bmjzyedkiyejcwah yiwlmreictznrsztpmxkodfkmd ehauhpxtdxxlzfajwk mzbhgqce odwkkoxraryloiturnbhrtiyrq svoyzg. Naigrhwvwvaeejrgq. Zrofee. Kpjfnp vnhvggukqauwrsk dqbhomvnefxtewllvrxm. Kmkzvgsyomxuywmitsanieglht wxszgavuutbpfnkzayjbotw. Szb halbkhacuxtnby. Dutvmnlfebyvcnaxybfdbhxnwn qartnhkfmyjjukemnjmhfgeaex nsalfauznt. Lcwumfbisgswgpukhuobzzbhbx tshjrkmcbqidy bflhmdstpynlugkwaadajuknxm achruioxq. Jenwpfvztvzwv. Gdlahexovgw. Abrtt. Xemkjzgusyfcwlfedma sndemzfapztjaytbusnbnse. Wbxzoypyikmgfnvwyxolnhdxue ba xcvmii. Dqidapafovwe. |
结果里看不到有明显的合法的单词。所以这种测试好像没有太多意义,多激活了几个脑细胞而已。在Google搜了搜,倒是有执着的people利用亚马逊强大的云计算平台,大规模测试这种随机性出现的单词在莎士比亚著作中的覆盖率,好像比较有效果,佩服之。
为了不浪费已经死掉的脑细胞,将这次的代码传到了github上(https://github.com/jma87/study/blob/master/shakespeare/random.c),但为了方便以后回顾和满足虚荣心,还是贴在这里比较实惠:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
#include <stdio.h> #include <stdlib.h> #include <time.h> #define true 1 #define false 0 #define QLEN 4 struct queue { char chq[QLEN]; unsigned qmem; }; int Inqueue(struct queue *ch, char cht) { int i; if(ch->qmem==QLEN) return 0; for(i=0;i<QLEN-1;i++) ch->chq[i]=ch->chq[i+1]; ch->chq[QLEN - 1] = cht; ch->qmem++; return ch->qmem; } int Outqueue(struct queue *ch) { int i; int cht=ch->chq[0]; ch->chq[0] = 0; ch->qmem--; return cht; } void Shakespeare(void) { char cht; struct queue *ch; unsigned i=0; int w_len=0; FILE *fp; if((fp=fopen("Shakespeare.txt","w+"))==NULL) { printf("can't open Shakespeare.txt, please chteck the FileSystem\n"); exit(0); } ch = (struct queue*)calloc(1,sizeof(struct queue)); srand((int)time(0)); while(true) { sw2: cht=rand()%29; sw: switch(cht) { case 26: //space if(ch->chq[3]==46 || isalpha(ch->chq[3])) { if (ch->qmem == QLEN) { cht = Outqueue(ch); putchar(cht); fputc(cht, fp); } cht=32; Inqueue(ch,cht); w_len=0; }else continue; break; case 27: //dot if(isalpha(ch->chq[3])) { cht=46; Inqueue(ch,cht); if (ch->qmem == QLEN) { cht = Outqueue(ch); putchar(cht); fputc(cht, fp); } cht = 32; Inqueue(ch, cht); w_len = 0; }else continue; break; case 28: //LF if(ch->chq[3]==32 && ch->chq[2]==46) { cht=10; Inqueue(ch,cht); w_len=0; }else continue; break; default: //alpha cht+=97; if (ch->chq[3] == 10 || (ch->chq[2] == 46) && (ch->chq[3] == 32) ||(ch->qmem==0)) cht = toupper(cht); Inqueue(ch, cht); w_len++; if(w_len>=26) { cht=26; goto sw; } break; } if(ch->qmem==QLEN) { cht=Outqueue(ch); putchar(cht); fputc(cht,fp); } } fclose(fp); } int main() { Shakespeare(); } |