Skip to main content
Engineering LibreTexts

4.7: Random Words

  • Page ID
    17054
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    To choose a random word from the histogram, the simplest algorithm is to build a list with multiple copies of each word, according to the observed frequency, and then choose from the list:

    def random_word(h):
        t = []
        for word, freq in h.items():
            t.extend([word] * freq)
    
        return random.choice(t)
    

    The expression [word] * freq creates a list with freq copies of the string word. The extend method is similar to append except that the argument is a sequence.

    Exercise \(\PageIndex{1}\)

    This algorithm works, but it is not very efficient; each time you choose a random word, it rebuilds the list, which is as big as the original book. An obvious improvement is to build the list once and then make multiple selections, but the list is still big.

    An alternative is:

    1. Use keys to get a list of the words in the book.
    2. Build a list that contains the cumulative sum of the word frequencies (see Exercise 10.7.3). The last item in this list is the total number of words in the book, n.
    3. Choose a random number from 1 to n. Use a bisection search (See Exercise 10.15.6) to find the index where the random number would be inserted in the cumulative sum.
    4. Use the index to find the corresponding word in the word list.

    Write a program that uses this algorithm to choose a random word from the book.

    Solution:

    http://thinkpython.com/code/analyze_book3.py


    This page titled 4.7: Random Words is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) .

    • Was this article helpful?