This JAVA,JGRASP is used!!!This question concerns developing a pseudo random num
ID: 666647 • Letter: T
Question
This JAVA,JGRASP is used!!!This question concerns developing a pseudo random number generator (PRNG). Java provides a perfectly serviceable PRNG (java.util.Random), however, building one is a useful OO exercise and offers an insight into these things are done.
A pseudo random number generator is a device that can produce or output a sequence of numbers that appear to be randomly selected.
One approach to developing a PRNG is based on the following general formula:
Xk+1=(gXk+c)mod(n)
(Note that the word "mod" denotes the modulo operation. For example, 5 mod 3 is 2.)
The formula describes a sequence of pseudo random values. The term Xk denotes one value in the sequence while Xk+1 denotes the next. Given a value in the sequence, we calculate the next by applying the formula. Customarily, a seed or start value, X0 is provided with which to kick the whole thing off.
According to this Wikipedia page, good values for the constants are:
c=0
g=48,271
n= 2311=2,147,483,647
Here's a specification for a RandomNumber generator class:
Class RandomGenerator
A RandomGenerator is a Pseudo Random Number Generator (PRNG). Its implementation is the Lehmer variant of linear congruential generator.
Instance variables
private long x;
// This field holds the most recently calculated value i.e. Xk.
Constructors
public RandomGenerator(long seed)
// Create a RandomGenerator object that uses the given seed as the value for X0.
Methods
public int nextInt()
// Return a pseudorandom integer value.
public int nextInt(int i)
// Return a pseudorandom integer value between 0 (inclusive) and the specified value (exclusive).
public double nextDouble()
// Return a pseudorandom real number value between 0.0 (inclusive) and 1.0 (exclusive).
Construct a RandomGenerator class of object that meets the given specification and is implemented using the given formula and constants.
Note the use of the data type long for the instance variable x. The numbers that the formula generates are very large integers hence this is the best choice.
Each ‘next…’ method should obtain a value for XK+1 and then use it to obtain the required result.
o The nextInt() method should obtain the result of casting Xk+1 as an integer.
o The nextDouble() method should obtain the result of dividing Xk+1 by n.
(You may wish to investigate the difference between, say, 34 and 34.0.)
o The nextInt(int i) method should obtain nextDouble(), scale it and cast it.
Here is an example of expected output:
Enter a seed value
7
Enter a parameter value for nextInt(int i):
10
Enter the number of calls required of each method.
5
nextInt(): 337897
nextDouble(): 0.5952271440044172
nextInt(10): 2
nextInt(): 518142577
nextDouble(): 0.7756899137868033
nextInt(10): 3
nextInt(): 1298864186
nextDouble(): 0.786058717307662
nextInt(10): 8
nextInt(): 439347582
nextDouble(): 0.6268343877172258
nextInt(10): 9
nextInt(): 162366641
nextDouble(): 0.6669665727144883
nextInt(10): 1
Note: you can comment out lines in the test harness, enabling incremental development of RandomGenerator methods.
Explanation / Answer
import edu.cornell.lassp.houle.RngPack.RanMT; /** * AbstractRandomGenerator based on RngPack RanMT generator. */ public class RandomGenerator { private RanMT random = new RanMT(); // private Ranlux random = new Ranlux(); private static RandomGenerator defaultRandom = new RandomGenerator(); public static RandomGenerator getDefault() { return defaultRandom; } public RandomGenerator() { } public RandomGenerator(long seed) { setSeed(seed); } public static void setDefaultSeed(long seed) { defaultRandom.setSeed(seed); } public void setSeed(long seed) { random = new RanMT(seed); // random = new Ranlux(seed); } public double nextDouble() { return random.raw(); } public double nextGaussian() { return random.gaussian(); } public int nextInt(int hi) { return random.choose(hi); } public boolean nextBoolean() { return random.coin(); } public static void main(String[] args) { RandomGenerator rngPackGenerator = new RandomGenerator(); for (int i = 0; i < 100; i++) { System.out.println(rngPackGenerator.nextDouble()); } } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.