next up previous
Next: About this document Up: Assignment 10: Tables Previous: Implement that hashtable

Random words

Goal: Write a method named randomWord in the WordCount class. It should choose and return a random word from the hashtable, such that the probability that each word appears is in proportion to its frequency in the original document.

  1. It might help to keep track of n, the total number of words in the document. Then you can choose a random integer in the range (0, n).
  2. As you traverse the keys in the hashtable, keep a running sum of the frequencies you have seen. When this total exceeds your random number, you've found your word.



Allen Downey
Mon Nov 13 09:42:26 EST 2000