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', 1 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', 1 of them will be 'b', 13 will be 'e', and so on.

This is left as a project. (Hint: consider a 1000-sided die with 81 faces marked 'a', 15 faces marked 'b', 127 marked 'e', and so on.)



Answer         Next Page         Previous Page Home