Add a new method to the random number class of the previous project. The new method generates

Question:

Add a new method to the random number class of the previous project. The new method generates the next pseudorandom number but does not return the number directly. Instead, the method returns this number divided by the modulus. (You will have to cast the modulus to a double number before carrying out the division; otherwise, the division will be an integer division, throwing away the remainder.)

The return value from this new member function is a pseudorandom double number in the range [0..1). (The square bracket, “[”, indicates that the range does include 0, but the rounded parenthesis, “)”, indicates that the range goes up to 1, without actually including 1.)


Data from Previous Project

In this project, you will design and implement a class that can generate a sequence of pseudorandom integers, which is a sequence that appears random in many ways. The approach uses the linear congruence method, explained below. The linear congruence method starts with a number called the seed. In addition to the seed, three other numbers are used in the linear congruence method: the multiplier, the increment, and the modulus. The formula for generating a sequence of pseudorandom numbers is quite simple. The first number is:

(multiplier * seed + increment) % modulus

This formula uses the Java % operator, which computes the remainder from an integer division. 

Each time a new random number is computed, the value of the seed is changed to that new number. For example, we could implement a pseudorandom number generator with multiplier = 40, increment = 3641, and modulus = 729. If we choose the seed to be 1, then the sequence of numbers will proceed this way:

First number

= (multiplier * seed + increment) % modulus

= (40 * 1 + 3641) % 729

= 36

and 36 becomes the new seed.

Next number

= (multiplier * seed + increment) % modulus

= (40 * 36 + 3641) % 729

= 707

and 707 becomes the new seed.

These particular values for multiplier, increment, and modulus happen to be good choices. The pattern generated will not repeat until 729 different numbers have been produced. Other choices for the constants might not be so good.

For this project, design and implement a class that can generate a pseudorandom sequence in the manner described. The initial seed, multiplier, increment, and modulus should all be parameters of the constructor. There should also be a method to permit the seed to be changed and a method to generate and return the next number in the pseudorandom sequence.

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Related Book For  book-img-for-question
Question Posted: