update 1, 1/25/2011:21:53

Homework Two: Random Number Generator

For this assignment, you will design and implement a pseudorandom number generator class. We are going to think of the of a random number generator as providing access to a Collection of numbers in a particular random order. Thus, your class should implement the Iterator interface (but of course you do not need to implement the remove() method) such that one can access random numbers from the sequence through the methods prescribed by the Iterator interface.

About random number generators

Computers cannot generate "truly" random numbers. Instead, they compute a sequence of "pseudorandom" numbers that appear to be random but which are generated deterministically. The basic approach is the linear congruence method. The sequence generated is dependent upon a seed, a multiplier, the increment, and the modulus. The formula for generating the next pseudorandom number in the sequence is:

(multiplier * seed + increment) % modulus

The seed is initially set by one of several schemes such as sampling the current time of day; for our purposes, it will be an input to the constructor. After the first pseudorandom number has been generated, the seed is always set to the most recently generated number. For example: with multiplier, increment and modulus values of 40, 3641 and 729 respectively, and an initial seed of 1, our first pseudorandom number would be:

(40 * 1 + 3641) % 729 = 36
and after accessing this first random number, the seed value will be 36. The next would be
(40 * 36 + 3641) % 729 = 707
with the seed at that point equal to 707. And so on.

Problem requirements

Design and implement a class, PRNG, that generates a sequence of (pseudo) random numbers in the manner just described. As mentioned above, we want your class to implement the Iterator interface. Your class should have at least two constructors, one that consumes a single int representing the intitial seed, and a second that consumes four int inputs, for initial seed, multiplier, increment and modulus (in that order).

The primary way we will want to interact with instances of the PRNG class will be through the next() method of the Iterator interface. Since we do not have a mechanism for removal of numbers from our sequence of random numbers, your implementation of the remove() method should simply throw an UnsupportedOperationException.

In addition to implementing the two constructors and three methods mandated by the Iterator interface, your class should provide a public method, setSeed(int newseed), which has the effect of (re)setting the seed to the given value whenever this method is called.

Generally, you will be expected to create your own driver class or include a main method in your classes. This should be a regular part of your development and testing. But for this assignment, I am providing this driver class, not so much as a substitute but to get you started. I recommend that you inspect this driver as in it I demonstrate several Java techniques including for and while loops, and the autoincrement operator.

Update 1: As always, you should fully document your program using javadoc directives. Similarly, you should generate the html files resulting from those comments and inspect them for completeness and correctness. Include these html files in your final submission.

Submission instructions:

Make sure that you create a folder named with your Westmont email name followed by "HW2". For example, someone with email address "cjones" would create a folder called "cjonesHW2". Make sure that inside that folder you have: java source files as well as html documentation files resulting from javadoc. Finally, either tar or zip the folder so that when we extract it, the folder "<emailname>HW2" will be created. Submit the packaged file for assignment 2 on Eureka.