Random Texts Do Not Exhibit the Real Zipf's Law-Like Rank Distribution

Background Zipf's law states that the relationship between the frequency of a word in a text and its rank (the most frequent word has rank , the 2nd most frequent word has rank ,…) is approximately linear when plotted on a double logarithmic scale. It has been argued that the law is not a relevant or useful property of language because simple random texts - constructed by concatenating random characters including blanks behaving as word delimiters - exhibit a Zipf's law-like word rank distribution. Methodology/Principal Findings In this article, we examine the flaws of such putative good fits of random texts. We demonstrate - by means of three different statistical tests - that ranks derived from random texts and ranks derived from real texts are statistically inconsistent with the parameters employed to argue for such a good fit, even when the parameters are inferred from the target real text. Our findings are valid for both the simplest random texts composed of equally likely characters as well as more elaborate and realistic versions where character probabilities are borrowed from a real text. Conclusions/Significance The good fit of random texts to real Zipf's law-like rank distributions has not yet been established. Therefore, we suggest that Zipf's law might in fact be a fundamental law in natural languages.


Introduction
Imagine that one takes a text, counts the frequency of every word and assigns a rank to each word in a decreasing order of frequency. This would result in the most frequent word having a rank of 1, the second most frequent word having a rank of 2 and so on. The histogram of such word ranks is said to conform to Zipf's law for word frequencies [1]. In its simplest form, the law states that f (r), the frequency of a word or rank r obeys where a is a constant, the so-called exponent of the law (typically a&1 [1]). In other words, Eq. 1 indicates that frequency decays linearly as the rank increases on double logarithmic scale.
Although the law was originally thought to reveal principles of language functioning [1], many have argued against its relevance [2][3][4][5][6][7]. Their major claim is that the statistics of simple random sequences of characters -including a special one that behaves as a word delimiter -reproduces Zipf's law for word frequencies [2,4,5]. Henceforth, we refer to this special character as a space or a blank. For instance, the random text wbqcrw h q rorjleabeyxkrlpqkpchnesguliwkb mrltn q a rss vfs w a h rlzpxxtxbkqetfwfpqudgwaorqwgqmo wyngwtbseuodboxaw x rldua eucx mmard xgqzv uu pueuerc pkizuauyrwi bllhjddv bp anud xbxvjyymioymvzebc tdtsecdijntssyepqdubcvxjd evavybwvejp w z uvspufvdvuzyf t nllifznwatic has been generated using English letters ranging from 'a' to 'z' (the separation between words in our example is arbitrary and due to automatic formatting). The idea that random sequences of characters reproduce Zipf's law stems from the seminal work of Mandelbrot [8] and was reformulated in various works [2,4,5,9]. We refer to a random sequence of characters of the type listed above as a random text so as to be consistent with [2] although more appropriate names have been discussed [10]. The simplest version of a random text is based upon the assumption that all characters are equally likely [2,7]. We define N as the number of regular characters of the random text and p b as the probability of a blank. The above example was generated with N~26 and p b~0 :18, which was deemed suitable for English [4,5]. It is noteworthy that when constructing the example above, we assumed that all characters are independent, that all letters from 'a' to 'z' are equally likely and two or more blanks in a row are not permitted. If two blanks in a row are not allowed then words with no characters (i.e. empty words) cannot be generated.
There have been many arguments against the meaningfulness or relevance of Zipf's law [2,4,6,7]. However, there are also reasons that such arguments might be flawed: The studies that question the relevance to natural language of Zipf's law argue for the matching between Eq. 1 and random texts. However, Eq. 1 is only an approximation for the real rank histogram. The best candidate for the actual rank distribution remains an open question [11][12][13][14] for two reasons: first, the goodness of the fit provided by Eq. 1 in a statistically rigorous sense is questionable and, second, the best function may not be unique [15]. If it turned out that when using statistically rigorous methods that real texts do not usually fit Eq. 1, then the arguments against the relevance of Zipf's law would be seriously challenged.

N Problem 2
As far as we know, in none of the popular articles that argue against the meaningfulness of Zipf's law [2][3][4][5][6][7] is there an accurate enough derivation of Zipf's law (Eq. 1) from random texts. This is of crucial importance because real texts and random texts may seem to have consistent rank distributions if not regarded with enough precision simply because two distinct tiny objects may look similar if our lens is not powerful enough. Notice that in [2][3][4][5][6][7] an exact derivation of Zipf's law from the assumptions of a random text is absent. Instead, only equations that are valid for the ensemble of words of a certain length are provided. For instance, Li [2] defines p w (L) as the probability of any particular word of length L and proves that where B, C and a' are constants and r(L) is the rank of any word of length L (a similar derivation can be found in [7]). Miller & Chomsky [4] showed that the probability of any word of length L obeys where B' and a'' are constants and r'(L) is now the mean rank of all the possible words of length L. In contrast, notice that Zipf's law (Eq. 1) is a law of individual ranks, not a law of a rank chosen to represent all words of the same length (e.g., the average rank or words of the same length). Recently, it has been proven that p(r), the probability of observing a word of rank r in a random text, obeys [16], for sufficiently large r, where c 1 and c 2 are two positive constants. Although the derivation of Eq. 4 in [16] for a general class of random texts is a milestone in the history of random texts, notice that Eq. 4 is weaker than the definition of Zipf's law in Eq. 1.

N Problem 3
Eqs. 2, 3 and 4 are derived in the context of a very long text. It is not known a priori if the parameters of the underlying exact distribution of ranks depend upon the text length or if the distribution that is obtained in the context of a very long text is the same as that of a random text of the size of the order of real texts.

N Problem 4
As far as we know, in none of the popular articles that question the meaningfulness to natural language of Zipf's law [2][3][4][5][6][7] is there any comparison between the rank histograms of actual texts and those of random texts. Rather it is simply taken for granted that an approximate agreement with Eq. 1 is sufficient.
To the best of our knowledge, in none of these cases is either a visual comparison between the rank histogram of a real text and that of a random text provided (e.g., by plotting both histograms together), nor are more convenient rigorous tests of the goodness of fit of random texts for real texts performed. In some exceptional cases, a visual comparison between a real text and an equation similar to Eq. 3 has been made [17] but the comparison implies the misuse of an equation that was originally derived for the mean rank of words of the same length to the individual ranks of actual Zipf's law-like rank distributions. Although Mandelbrot did not show simultaneously real and artificial rank distributions, arguably he inappropriately used equations that had been derived for individual ranks (e.g., Fig. 1 of [18] and Fig. 2 of [8]).
To address Problem 1, we evaluate the goodness of fits of random texts to real texts directly by means of samples of ranks produced by the real process and not indirectly through Eq. 1. To address Problem 2 we study the consistency between rank samples from a random text and rank samples from a real text using three rigorous statistical tests. We skip the mathematical challenge of obtaining the missing exact rank distribution for individual ranks. To address Problem 3, we compare real texts with random texts of the same length. In this way, we can establish that putative differences cannot be attributed to simply differences in the text length. To address Problem 4, we compare visually the rank histogram of random texts with those of real texts so as to provide an estimate of the enormous differences between both and then we perform rigorous statistical tests to show that the real word rank histograms are inconsistent with those of random texts.
We exclude from our analysis a variant of the random text that generates empty words. Empty words are obtained when producing two blanks in a row, which is allowed in [4][5][6][7]16] but not permitted in Li's version [2] (see Text S1). In other cases, it is not clear if empty words are allowed, e.g., [19]. Excluding empty words in our study is justified by the fact that the goal of this article is to evaluate the fit of random texts for real Zipf's law-like word rank distributions. As far as we know, in none of Zipf's pioneering works [1,20] and in the many studies that followed, have empty words been included or even considered in real rank histograms. Indeed, their existence in real texts is very questionable.
Many authors have discussed the explanatory adequacy of random texts for real Zipf's law-like word rank distributions indirectly from inconsistencies between random texts and real texts beyond the distribution of ranks [19,[21][22][23][24]. One of the most typical and recurrent examples is the claim that real word lengths are not geometrically distributed as expected from a random text experiment [21][22][23][24]. However, the question that we seek to address here is: do random texts really fit the real Zipf's law-like distribution accurately as suggested by many [2,[4][5][6][7]25]?
To our knowledge, only a few studies have addressed this question [19,26,27] but in a qualitative manner and only for certain versions of the random text model. In this article, we go a step forward by bringing rigorous statistical tests into the debate and considering all the variants of the random text model that have been considered in the literature. In particular, we compare visually some rank histograms from English texts with those of different versions of the random text model and test rigorously the goodness of fit of random texts on actual histograms in a set of ten texts. We demonstrate that -contrary to what has previously been suggested -random texts fail to fit actual texts even visually.
Finally, we shed light on the failure of random texts to fit actual texts from the perspective of cognitive science and discuss the implications of our negative results for the meaningfulness to natural language of Zipf's law.

Results
In this article we employ a set of ten English texts (eight novels and two essays) to evaluate the goodness of fit of random texts in Table 1. A summary of their statistical properties is shown in Table 2.

The Versions of the Random Text
We consider three different versions of the random text (RT) model without empty words that have been considered in the literature. All the versions generate a random sequence of independent characters. These three version are (the subindex indicates the number of parameters of the version of the random text): All characters, including the blank are equally likely. This model is specified with a single parameter: N, the number of characters other than space. N [ f2,4,6,26g was used in [2]. N~5 was used by [7] allowing empty words. An example of RT 1 with N~2 is uu kuuuuk k kkk uu u kkuuukuuk uk kukukuuu u ukku kukkk uku uku ku u kuk kukk uuuk k kk kku uuu u kuukkuk u kku kuukuu u uukk ku uuk kukk u ukkkkuuu k ukku kuku kuk k k uku k uuku uu kuukukuukk kukku k uk u All characters except the blank are equally likely. This model is specified with two parameters, N as in RT 1 plus p b , the probability of blank for the 2nd and following characters of a word (notice that in our case, the probability that the current word under construction has no character when the blank is produced is zero). Allowing empty words, N~26 and probability of blank p b~0 :18 was argued to be suitable for English [4,5] without explaining how p b was estimated. Here we obtain N and p b from real normalized texts (see Materials and Methods for details about our text normalization). N is obtained from the number of different characters of the text (except the blank). p b is computed from the formula where N b is the number of blanks and N c is the total number or characters (including blanks). In our text normalization, N b is equivalent to the number of different words (i.e. the maximum rank). p b is the proportion of blanks after excluding the first character of each word, which cannot be a blank in our versions of the random text model. An example of RT 2 with N and p b borrowed from Alice's adventures in wonderland is i 0xbple f h gxadchrdcty hz trsykj o b axurvg qfu k kg3kx vwzsj3 xw0t3f nq ryb uhibb nqhtqb zfgnfk v gdq p30ajh 30 c p k3cgozfe3vt hdmzc k0q bw fs c kgu lm0tx bh av eu v cmbosjbis a3aks mucjtefrtvko t uyprnz eyti h3do hm0mx w0kbecyd ti v qoyowzcfiykv3wb All characters can take any probability. This model is specified with Nz1 parameters (i.e. the Nz1 probabilities of each of -Real Real character probabilities extracted from the target writing as in [19]. An example of RT Nz1 with real character probabilities borrowed from Alice's adventures in wonderland is tel g shs oo fagl t ersu fa r esnrlaod k ni ihe a o e sh foie r do aorhdaev aiot t oseldtiyie wq t thsynt w e sptsnsm heooeat utdgeco a iyeb sniemt ehdoy t thruw twaame eatendeisidle mc nlhtt ih a utfd anulbgleta nlh ohe gt eehitofnet

Visual Fitting
Here we aim to compare rank histograms from real texts and expected rank histograms from random texts. If random texts really reproduce the rank histogram of real texts, then the histogram of real texts and those of the random texts should completely overlap. We will see that this is not the case.
Here our emphasis is on providing a fair visual comparison. We use the term fair in two senses. First, we consider real and artificial texts of the same length in words. Notice that the equations that have been derived so far for the rank distribution of RT 1 and RT 2 texts are derived in the limit of a very large text in which all words of the same length must have the same frequency of occurrence because they are a priori equally likely [2,4,5,7]. If the text is not long enough, the frequency of words of the same length may differ noticeably. Here we aim to equate the text size of both the model and the real text. Second, we do not misuse a theoretical equation that is not valid for individual ranks as in [17]. The theoretical rank distribution or even the theoretical expected rank histogram of random texts are not available, even in their most simples versions. Therefore, we work on the expected rank histogram of random texts, which can be easily estimated by simulating the process and averaging the rank histogram over a sufficiently large number of artificial texts. Third, we do not use binning as in [26] which could shadow the differences between actual texts and random texts.
In the interest of being concise, for visual fits, we chose four works representing different genres and covering the whole range of text lengths in the sample. Fig. 1 shows the rank histogram of the four selected English texts versus the expected rank histogram of a RT 1 . From visual inspection, it is obvious that the agreement between the random text and the real text is poor. The histograms of random texts are clearly above the corresponding real histograms for small ranks and clearly below for larger ranks. Additionally, the curves of real histograms are smoother as compared to the pronounced staircase decrease of random texts, especially for small ranks. Fig. 2 shows that RT 2 with N and p b taken from the real text does not improve the quality of the visual fit. The staircase decrease of the histogram of random texts becomes more radical and the plateaus are huge as compared to those of Fig. 1. One may infer from Figs. 1 and 2 that RT 1 gives a better fit than RT 2 in general, but the difference in fitting is mainly due to the small value of N employed in Fig. 1, which produces smaller plateaus with regard to Fig. 2.
It is well known that if characters other than the blank have unequal probabilities then the rank histogram smoothes [2,25,28]. The point is: would this apparently dramatic improvement be enough to achieve a perfect fit? Fig. 3 shows that these random texts (RT Nz1 model) still deviate from the real texts from which they borrow the character probabilities. For instance, Fig. 3 shows that random texts display pronounced humps for high frequencies and wider plateaus in the low frequency domain with regard to the real text. Besides, the rank histogram of random texts is clearly longer than that of real texts in Figs. 2 and 3. The fact that the plateaus at the low frequency region are much broader for RT Nz1 texts than for real texts is well-known [19]. In the next section we show that the differences between real texts and random texts with non-commensurate character probabilities are statistically significant as well as for all the parameters suggested in the literature for RT 1 and RT 2 .
In the next section, we employ rigorous statistical fitting, not because we think that it is strictly necessary when large visual differences between random and real texts are found (e.g., Figs. 1 and 2), but so as to provide a foundation for a more mathematically precise understanding of the differences between real texts and random texts and to extend, in a concise way, the analysis to the texts and parameters settings not considered in the figures. Notice that the poor visual fit of random texts shown in Figs. 1, 2 and 3 also applies to the real texts in Table 1 not visually examined in these figures so as to conserve space.

Rigorous Statistical Fitting
We detailed in the introduction that we did not seek to evaluate the goodness of fits of random texts for actual rank histograms through Zipf's law because this implies the risk that the target equation, i.e. Eq. 1, is not accurate enough [11,14]. A typical way of testing the fit of a certain model to real data is from the exact distribution that characterizes the model [12]. However, as mentioned in the introduction, this is impossible in the current situation because the exact rank distribution of random texts is unknown. To our knowledge, only approximations have been derived. Furthermore, the intention of our article was not to derive this equation per se. In light of the absence of such an exact distribution, we evaluate the consistency of ranks from a real text with those of a random text of the same length (in words) through three different statistics of the rank r in a text of length T: N max (r), the maximum rank. max (r) is the observed vocabulary size and measures the width of the rank histogram). Notice that the actual vocabulary of a random text model is infinite (a priori, any string of at least one letter can be formed), contrary to the actual vocabulary of a writer, which although large is finite. Support for max (r) comes from previous work suggesting that the pattern of observed vocabulary growth is useful for distinguishing between natural and RT Nz1 texts [19]. We find that max (r) is indeed useful for any kind of random text and that simply its value is enough to distinguish random texts from real texts for the versions and parameter settings considered in this article.
N m(r), the mean rank. N s(r), the standard deviation of the rank.
To our knowledge, the expectation of these statistics for a text of a certain finite length has not previously been reported. If the rank distribution of the real texts and that of the random texts are the same, statistically significant differences between the value of the above statistics in real texts and those of random texts should not be found or be exceptional. Here we consider the whole set of ten English texts including the four works we examined in detail in the previous section (Table 1).
For each real text, we estimate the expectation and standard deviation of these statistics by generating 10 4 independent random texts for all the versions and parameters of the random text reviewed above. Notice that the length in words of the random texts is the same as that of the real text. Then we calculate k, the distance to the mean (measured in units of the standard deviation) between the value of real value of the statistic in the target text and that of a random text of a certain version and parameter setting. The three rank statistics yield three distances, i.e., k max (r)~m ax (r){m( max (r)) s( max (r)) ð6Þ k m(r)~m (r){m(m(r)) s(m(r)) ð7Þ The sign of the distance indicates whether the actual value is smaller than the expected (kv0) or larger than expected (kw0) for the hypothesis of a random text. Table 3 shows a summary of these signed distances for the texts in our data set. How can we determine the significance of these distances? The Chebyshev inequality provides us with an upper bound of the, pvalue, the probability that the value of the distance is due to mere chance for any kind of distribution. This upper bound is 1=DkD 2 , where D:::D is the absolute value operator [29]. Henceforth we use the term absolute distance to refer to DkD. We estimate the mean (m) and standard deviation (s) that are needed to compute the distances (Eqs. 6, 7 and 8) by simulating the version of the random text with the parameter setting under consideration a certain number of times (10 4 in our case). Table 3 shows, that all absolute distances (for any novel, any version of the random text and any parameter setting) are above 36:8. The minimum absolute distance is achieved by RT Nz1 for CC with the parameters setting L 2 and the statistic m(r). This means that the distance p-values, in all cases do not exceed 1=36 2 &8 : 10 {4 . Next we examine some concrete examples of the huge distance between a real text and a certain random text model and parameter setting using the results in Table 3. The minimum absolute distance achieved by any statistic for: N the fair die rolling experiment considered in [7] (RT 1 with N~5) is 76:8 standard deviations, which is achieved by the text CC (A Christmas carol, by Dickens). This means that the pvalue of the differences for any statistic and for all texts does not exceed 1=76:8 2 &2 : 10 {4 . In our version of the model, we do not allow for empty words to make the model more realistic.
N the variant of the random text model considered in [4] (RT 2 ) is 93:1 standard deviations, which is achieved by the text AAW (Alice's adventures in wonderland, by Carrol). This means that the p-value of the differences for any statistic and for all the texts does not exceed 1=93:1 2 &10 {4 . In our version of the model, we do not allow for empty words to make the model realistic and estimate the parameters from the real text.
N the random text with unequal letter probabilities (RT Nz1 with the three different parameters settings) is 36 standard deviations, which is achieved by CC with the parameter setting L 2 (this is the minimum distance for all versions of the random texts and parameter settings). Thus, the p-value of the differences for all statistics and for all the tests does not exceed 1=36 2 &8 : 10 {4 . This is striking, since it has been claimed that unequal letter probabilities improve the fit of random texts to the rank distribution of real texts dramatically [25]. In contrast, we show that the hypothesis of a random text is still rejected with unequal letter probabilities.
Next we focus on the sign of the distances in order to shed light on the nature of the disagreement between real and random texts. The sign of the distance indicates whether the actual value is too small (kv0) or too large (kw0) for the hypothesis of a random text. In all the cases shown in Table 3, the sign of this new distance is negative (the real values of the statistic are too small) except for RT 1 with N~2 and RT Nz1 with the parameters setting L 1 , where that distance is positive (the real values of the statistic are too large in these cases). A further statistical test confirming the results obtained thus far is presented in Text S1.

Discussion
We have seen that three different rank statistics are able to show, independently, that ten English texts and random texts with different versions and parameters settings are statistically inconsistent in all cases. We have seen that for the majority of the parameter settings considered, the nature of the disagreement is that the real rank statistic is smaller than that expected for a random text.
Although we have shown the poor fits of random texts by means of rigorous statistical tests, our limited exploration of the parameter space cannot exclude the possibility that random texts provide good fits for actual rank histograms with parameter values not considered here. Notice that random texts fail both with arbitrarily chosen parameters, e.g., the fair die rolling experiment [7] with N~5 and p b~1 =6 (model RT 1 ), and with parameters inferred from the target text, which would seem a priori more likely to yield a good fit. Despite our limited exploration of the parameter space, in the absence of concrete parameter values for which random texts fit real rank histograms accurately, the meaningfulness for natural languages of Zipf's law-like word distributions remains viable.
We believe that the quest for parameters that provide a good fit of random texts on real texts is a tough challenge for detractors of the meaningfulness of Zipf's law, because real writers do not produce words by concatenating independent events under a certain termination probability. Real writers extract words from a mental lexicon that provides almost 'ready to use' words [30]. Our main point here is that generally the lexicon provides root word forms that can be completed with affixes. The valid root forms are basically determined a priori. Although writing can be a very creative exercise, real writers do not construct words 'on the fly' as in the random texts that have previously been presented as an argument against the utility of probability distributions in language. Although some writers do invent many words, their creativity is limited by the need to be understood by their readers. Indeed, the meaning of invented words has to be guessed from the surrounding words. If the context words are also invented, then the reader is likely to get completely lost. Considered from this perspective, random texts are a case of maximum word creativity, and are not limited by a need to be understood (recall the meaningless examples of random texts in the Introduction and the Results section) but only constrained by the prior character probabilities.
There are still many models of Zipf's law for which the goodness of fit to real texts has not been studied rigorously (e.g., [31,32]). A remarkable exception is [21]. Further research is necessary in Table 3. Distance to the mean in standard deviations. Summary of k, the distance to the mean (in standard deviations), between real values and those of random texts for three different rank statistics: max(r) (the maximum rank), m(r) (the mean rank) and s(r) (the standard deviation of the rank). The first column contains the abbreviation of the text (see Table 1 for the meaning of each abbreviation). Texts are sorted by increasing length. The columns after the first column correspond to different versions of the random text model and different parameter settings. For each text and parameter setting, we show k max (r) , k m(r) and k s(r) , the distances from each of the three rank statistics. N is the number of characters other than space. L 1 and L 2 are two parameter settings borrowed from [2]. Real indicates that all character probabilities are obtained from the original text. Distances are computed from the estimated mean and standard deviation of the rank of a certain random text through 10 4 independently generated replicas. The random texts have the same length in words as the target real text. doi:10.1371/journal.pone.0009411.t003 order to establish which models provide the best fit in a statistically rigorous sense. Indeed, this is yet another reason to conclude that two fundamental research problems about Zipf's law in natural languages, namely its meaningfulness and a realistic explanation of it, remain open.

Materials
To simplify the analysis, we normalize the English texts in Table 1 by removing all marks, lower casing all letters, converting all spaces into blanks and leaving only one blank after each word. In this way, we obtain a sequence of words whose length is at least one character and separated by a single blank. A similar normalization procedure is used in [19] although this study does not provide enough details to determine if its normalization procedure is exactly the same as ours.
After text normalization, there is a small fraction of word characters that are not letters in the English alphabet. Most of these characters are digits or accents. To make sure that our results are not a consequence of these infrequent characters we repeated the fitting tests excluding words not made exclusively of English lowercase letters from 'a' to 'z' after text normalization. We found that the results were qualitatively identical: each of the three rank statistics is able to reject the hypothesis of a random text in all cases.

Computational Methods
Here we aim to provide some guidelines to perform the computer calculations presented in this article for easy replication of our results. In what follows we consider the computational efficiency of three issues: (i) the generation of random words; (ii) counting the frequency of random words; (iii) and sorting.
Random word generation. Here we explain how to generate a random word efficiently. We start with the simplest (or naïve) algorithm of random word generation (we assume that the space delimiting words does not belong to the word): 1. Start with an empty string of characters s. 2. Generate a random character c and add it to s. 3. Generate a uniform random deviate x*U(0,1). 4. While x §p b do 5. Generate a random character c and add it to s. 6. Generate a uniform random deviate x*U(0,1).
Generating a uniformly distributed random letter (steps 2 and 5) for the models RT 1 and RT 2 with a standard random uniform given the alphabet size N is straightforward. Generating a random letter for RT Nz1 where probabilities come from a real text can also be easily performed in h(1) time using a table look-up method [33]. If character probabilities come from a real text (as in the parameter setting Real (Results section), we can place all the characters other than space from that text in a table and then choose one uniformly in h(1) time. This needs space h (N c {N b ), where N c is the number of characters of the text after text normalization (including blanks) and N b is the number of blanks. If the character probabilities are given a priori but are rational numbers (as in the parameter settings L 1 and L 2 borrowed from [2]), then we can generate a table where the relative frequency of each character is the same as the desired probability. Alternatively, for the case of RT Nz1 in general, one can use an inversion method with a guided table [33]. The fact that normally N%N c {N b in large enough texts implies that this inversion method requires less space than the table look-up method while keeping the h(1) time for generating a random letter.
Imagine that a random word has length L (we assume L §1 in our random texts). The naïve algorithm above needs invoking a random uniform deviate generator 2L times, i.e. L times for generating each of the L random characters (steps 2 and 5) and L times for determining if more characters have to be added or not (steps 3 and 6). We can reduce the number of random uniform deviates that need to be generated using the following algorithm: 1. Generate a random geometric deviate L*G(p b ). 2. Generate a random word w of length L, where is Step 2 is performed through the following algorithm 1. Start with an empty string of characters s. 2. Repeat L times 3. Generate a random character c and add it to s.
Of key importance is the generation of the random geometric deviate in h(1) time. It is possible to generate a random geometric deviate L with parameter p b (L §1) from a random uniform deviate x through the formula [33,34] ð9Þ where l~log (1{p b ). To save computation time, the constant l is calculated only once. The second version of the algorithm needs to generate only Lz1 uniform deviates (one for generating the geometric deviate and L for each of the L characters comprising the word) whereas the naïve first version required 2L uniform deviates.
It is still possible to generate a random word of length L with only L uniform deviates. The idea is to allow the blank to be among the characters that can be generated once the first nonblank character has been placed. The algorithm is 1. Start with an empty string of characters s. 2. Generate a random character c (c cannot be a blank) and add it to s. 3. Generate a random character c (possibly a blank). 4. While c is different than blank do 5. Add c to s. 6. Generate a random character c (possibly a blank).
Word frequency counting. We define T as the length of a text in words. By ignoring the length of a word, the frequency of a word efficiently can be counted in h(1) time and h(T) space using a hashing table [35] of character strings.
With simultaneous random word generation and counting, the time efficiency can be improved by employing more memory for the case of RT 1 and RT 2 . The idea is to keep the hashing table only for counting the frequency of the words of lengths greater than L max and using a matrix F~ff ij g for counting the frequency of each of the N i words of length i such that 1ƒiƒL max . f ij is the frequency of the j{th word of length i with 1ƒjƒN i . In this way, a random word of length L max or smaller can be simultaneously generated and counted involving only two random deviates with the following simple algorithm: 1. Generate a random geometric deviate i*G(p b ) 2. If iƒL max then 3. Generate a random uniform number j*U½1,N i . 4. Increase f ij by one. 5. else 6. Generate a random word w of length i by means of the algorithm above. 7. Increase the frequency of w by updating the hashing table of character strings.
The extra memory needed for the table of words of length not exceeding L max is Sorting. Sorting natural numbers efficiently is needed to calculate ranks. Obtaining the ranks of a certain text (real or random) requires sorting the word frequencies from the random text in decreasing order. All the above techniques may not contribute to increase significantly the speed of the computer calculations if the sorting takes more than h(T) time, where T is the length in words of the text. In our case, we can take advantage of the fact that frequencies lie within the interval ½1,T and then we can use counting sort [35], which allows one to sort elements in linear time.