Puzzle R11

Generate and print random characters 'a' .. 'z'

[M-1] Write function char randChar() that evaluates to a random character 'a'..'z' each time it is called. Do this by adding a random integer to character 'a'.

[M-20]Print out about 500 of these characters, nicely arranged into groups of five. Sample output:

aofvp mjxvt ewsnh acjde zldaa jnopp erljb puunh wsyyo dmgwf
uvzzp kghva jcrba xhhpr vsmft mlytc pktpo jdflu nztie rmbsn
dydxs hlbzr dwvpe evmen tkhor tsmdj vanrl cyxoi mjwil hzhto
ftvkn xazob nfvqr fvdct iyhid tvspt gdabu wfdoa cltro blfsh
lgpnq enzsy eiezl zcqcl ybxhf tkfqp lmpqw vqsoj etoxg epspj
mctpq rudow xpsbr ziohe cwwte icbqv olkhm nditi vywsh ydbun
pybwq icnfh xorcm jmunj rnpka scqwm tmjuo jyqej dtypa ibqdw
wpswa dsqfb eqiij ruunp uxdqk gdwbl ohofi cvdhu xutpj wfwfg
ozbcn lcgxg ddycb bixkl yvnvs kgany kgpgr ylxay puodf jakcr
wbvrr unrdr suawq scoyb hdzqh xjsfe gnxcx lcuez uwfla tckiz

The output part of this program is the hardest part. Skip it, or use simpler output if you want. Use randInt() from puzzle 6 for generating the characters.

PROJECT With char randChar() every character 'a' through 'z' is equally likely. The result does not look like English, where characters occur at different frequencies. Here is a table that shows the relative frequency of English characters:

LetterFrequency
a 8.167%
b 1.492%
c 2.782%
d 4.253%
e 12.702%
f 2.228%
g 2.015%
h 6.094%
i 6.966%
j 0.153%
k 0.772%
l 4.025%
m 2.406%
n 6.749%
o 7.507%
p 1.929%
q 0.095%
r 5.987%
s 6.327%
t 9.056%
u 2.758%
v 0.978%
w 2.360%
x 0.150%
y 1.974%
z 0.074%

So out of 100 characters in English text, about 8 of them with be 'a', 2 of them will be 'b', 13 will be 'e', and so on.

Write char randCharFreq() that randomly generates characters with the same frequency as English. Characters will be picked randomly, but out of 100 characters, about 8 of them with be 'a', 2 of them will be 'b', 14 will be 'e', and so on.

This is left as a project. (Hint: consider a 100-sided die with 8 faces marked 'a', 2 faces marked 'b', 14 marked 'e', and so on.) Low frequency characters have just one face. Here is what this impoved program outputs:

osoat aucrc isbdw shint efdea buton ahtew ndnah dsjia uwesf
oxsje sbtrn leehs eitdm mbkal elwet elfit eucnc tepqm aoetu
dpdsc wwnhs entil ateha trsks aieqt ienhb heqkp urrtv funle
toebs ekeoe legca kxnoh erwas abnio hhlmo pbeot wsltr ebpde
nomni myddt nword aaeis ehrtw aetil eocfe bhhmr dhbtf atetr
ifiha osebh ndeal ewlhk rftlp aeahm nrrll fhioo uxeth aaaes
dfnij soehs rleee eieqh nyeoe tesre orshk greau srsai nroen
dtori hnahb etees exdgf ijtee jeeif iqoee hmlne bhosr hcxei
sadtr eulas tmsem xqicw docqt iwwtk adjts neamo cbntf unios
bspta mavtw rshls reeaa rapen tncut crqru dokfh eoeud salss

Possibly it looks a little bit more like English.



Previous Page        Answer         Next Page         Home