(*******************************************************************
This file was generated automatically by the Mathematica front end.
It contains Initialization cells from a Notebook file, which
typically will have the same name as this file except ending in
".nb" instead of ".m".

This file is intended to be loaded into the Mathematica kernel using
the package loading commands Get or Needs.  Doing so is equivalent
to using the Evaluate Initialization Cells menu command in the front
end.

DO NOT EDIT THIS FILE.  This entire file is regenerated
automatically each time the parent Notebook file is saved in the
Mathematica front end.  Any changes you make to this file will be
overwritten.
***********************************************************************)



<< Graphics`Graphics`



randomletter[a_, q_] :=
  Block[{r},
             r = Random[];
             If[r > 1-q,
                    Return[0],
                    Return[Ceiling[a r/(1-q)]]
                  ]
    ]









Clear[randomwordlist];
randomwordlist[a_, q_, n_] :=
  Block[{wordlist = {},
                  currentletter,
                  currentword = {},
                 i,
                 wordcount = 0
                },
                While[wordcount < n,
                            currentword = {};
                           currentletter = randomletter[a, q];
                          While[currentletter \[NotEqual] 0,
                                         
        currentword = Append[currentword, currentletter];
                                          currentletter = randomletter[a,q];
                                        ];
                            If[Length[currentword] > 0,
                               wordlist = Append[wordlist, currentword];
                               wordcount++
                           ];
                       ];
              Return[wordlist]
             ]

























ithword[i_, k_, a_] :=Return[IntegerDigits[i-1, a, k]+ 1]








frequencycount[wordlist_, a_, k_] :=
  Block[{frequencies = {},
                   i
                 },
              For[i = 1, i \[LessEqual] a^k, i++,
                        
      frequencies = Append[frequencies,Count[wordlist, ithword[i, k , a]] ]
                      ];
              Return[frequencies]
              ]















freqcount[wordlist_] :=
  Block[{outputlist = {},
                  currentword,
                  currentplace= 1,
                 currentcount = 1
      },
                currentword = wordlist[[1]];
                currentplace = 2;
                While[currentplace \[LessEqual] Length[wordlist],
                               
      If[wordlist[[currentplace]] \[Equal] currentword,
                                         currentcount++;
                                         currentplace++,
                                          
          outputlist = Append[outputlist, currentcount];
                                          
          currentword = wordlist[[currentplace]];
                                         currentcount = 1;
                                         currentplace++;
                                       ];
                             ];
                 outputlist = Append[outputlist, currentcount];
                 outputlist = Reverse[Sort[outputlist]];
                Return[outputlist]
               ]















freqprob[freqlist_] :=
  Block[{outputlist = {},
                  currentfreq,
                  currentplace= 1,
                 currentcount = 1,
                numfreqs = Length[freqlist]
      },
                currentfreq = freqlist[[1]];
                currentplace = 2;
                While[currentplace \[LessEqual] Length[freqlist],
                               
      If[freqlist[[currentplace]] \[Equal] currentfreq,
                                         currentcount++;
                                         currentplace++,
                                          
          outputlist = 
            Append[outputlist, {currentfreq,currentcount/numfreqs}];
                                          
          currentfreq = freqlist[[currentplace]];
                                         currentcount = 1;
                                         currentplace++;
                                       ];
                             ];
                 
    outputlist = Append[outputlist, {currentfreq, currentcount/numfreqs}];
                 outputlist = Reverse[Sort[outputlist]];
                Return[outputlist]
               ]



twoplots[m_, q_, n_] :=
  Block[{wordlist = {},
                    freqlis = {},
                   freqcountlis = {}},
                  wordlist = randomwordlist[m, q, n];
                 freqlis = freqcount[Sort[wordlist]];
                 
    freqlis2 = Transpose[{Table[i, {i, Length[freqlis]}], freqlis}];
                LogLogListPlot[freqlis2, PlotRange \[Rule] All];
                freqcountlis = freqprob[freqlis];
                LogLogListPlot[freqcountlis, PlotRange \[Rule] All];
                 ]





