Question

Generating pseudo-random numbers. Develop a data type for generating pseudo-random numbers. That is, convert StdRandom to...

Generating pseudo-random numbers. Develop a data type for generating pseudo-random numbers. That is, convert StdRandom to a data type. Instead of using Math.random(), base your data type on a linear congruential generator. This method traces to the earliest days of computing and is also a quintessential example of the value of maintaining state in a computation (implementing a data type). To generate pseudo-random int values, maintain an int value x (the value of the last “random” number returned). Each time the client asks for a new value, return a*x + b for suitably chosen values of a and b (ignoring overflow). Use arithmetic to convert these values to “random” values of other types of data. As suggested by D. E. Knuth, use the values 3141592621 for a and 2718281829 for b. Provide a constructor allowing the client to start with an int value known as a seed (the initial value of x). This ability makes it clear that the numbers are not at all random (even though they may have many of the properties of random numbers) but that fact can be used to aid in debugging, since clients can arrange to see the same numbers each time.

Please use java to complete

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PseudoRandom.java

=============================

package random;

public class PseudoRandom {
   // use formula return = a*x + b for linear congruential generator
  
   // data field
   // the value of the last “random” number returned
   private int int_x;
   private long long_x;
   private float float_x;
   private double double_x;
  
   // values suggested by D. E. Knuth
   private final long a = 3141592621L;
   private final long b = 2718281829L;
  
   // constructor
   public PseudoRandom(int seed) {
       // initialize data fields
       int_x = seed;
       long_x = seed;
       float_x = seed;
       double_x = seed;
   }
  
   public int nextInt() {
       int_x = (int) (a*int_x + b);
       return int_x;
   }

   public long nextLong() {
       long_x = a*long_x + b;
       return long_x;
   }

   public float nextFloat() {
       float_x = a*float_x + b;
       return float_x;
   }
  
   public double nextDouble() {
       double_x = a*double_x + b;
       return double_x;
   }

   public boolean nextBoolean() {
       int_x = (int) (a*int_x + b);
       return (int_x) % 2 == 0;
   }
}


===================================

Tester.java

===================================

package random;

public class Tester {

   // main method to test Pseudo Random generator
   public static void main(String[] args) {
       // create a PseudoRandom object
       PseudoRandom prand = new PseudoRandom(326); // seed 326
       // generate 5 numbers for each type
       System.out.println("Integer: " + prand.nextInt());
       System.out.println("Integer: " + prand.nextInt());
       System.out.println("Integer: " + prand.nextInt());
       System.out.println("Integer: " + prand.nextInt());
       System.out.println("Integer: " + prand.nextInt());
      
       System.out.println("Long: " + prand.nextLong());
       System.out.println("Long: " + prand.nextLong());
       System.out.println("Long: " + prand.nextLong());
       System.out.println("Long: " + prand.nextLong());
       System.out.println("Long: " + prand.nextLong());
      
       System.out.println("Float: " + prand.nextFloat());
       System.out.println("Float: " + prand.nextFloat());
       System.out.println("Float: " + prand.nextFloat());
       System.out.println("Float: " + prand.nextFloat());
       System.out.println("Float: " + prand.nextFloat());
      
       System.out.println("Double: " + prand.nextDouble());
       System.out.println("Double: " + prand.nextDouble());
       System.out.println("Double: " + prand.nextDouble());
       System.out.println("Double: " + prand.nextDouble());
       System.out.println("Double: " + prand.nextDouble());
      
       System.out.println("Boolean: " + prand.nextBoolean());
       System.out.println("Boolean: " + prand.nextBoolean());
       System.out.println("Boolean: " + prand.nextBoolean());
       System.out.println("Boolean: " + prand.nextBoolean());
       System.out.println("Boolean: " + prand.nextBoolean());
   }
}

let me know if you have any problem or want any modification in class. thank you.

Add a comment
Answer #2

Answer:-

PseudoRandom.java

=============================

package random;

public class PseudoRandom {
   // use formula return = a*x + b for linear congruential generator
  
   // data field
   // the value of the last “random” number returned
   private int int_x;
   private long long_x;
   private float float_x;
   private double double_x;
  
   // values suggested by D. E. Knuth
   private final long a = 3141592621L;
   private final long b = 2718281829L;
  
   // constructor
   public PseudoRandom(int seed) {
       // initialize data fields
       int_x = seed;
       long_x = seed;
       float_x = seed;
       double_x = seed;
   }
  
   public int nextInt() {
       int_x = (int) (a*int_x + b);
       return int_x;
   }

   public long nextLong() {
       long_x = a*long_x + b;
       return long_x;
   }

   public float nextFloat() {
       float_x = a*float_x + b;
       return float_x;
   }
  
   public double nextDouble() {
       double_x = a*double_x + b;
       return double_x;
   }

   public boolean nextBoolean() {
       int_x = (int) (a*int_x + b);
       return (int_x) % 2 == 0;
   }
}


===================================

Tester.java

===================================

package random;

public class Tester {

   // main method to test Pseudo Random generator
   public static void main(String[] args) {
       // create a PseudoRandom object
       PseudoRandom prand = new PseudoRandom(326); // seed 326
       // generate 5 numbers for each type
       System.out.println("Integer: " + prand.nextInt());
       System.out.println("Integer: " + prand.nextInt());
       System.out.println("Integer: " + prand.nextInt());
       System.out.println("Integer: " + prand.nextInt());
       System.out.println("Integer: " + prand.nextInt());
      
       System.out.println("Long: " + prand.nextLong());
       System.out.println("Long: " + prand.nextLong());
       System.out.println("Long: " + prand.nextLong());
       System.out.println("Long: " + prand.nextLong());
       System.out.println("Long: " + prand.nextLong());
      
       System.out.println("Float: " + prand.nextFloat());
       System.out.println("Float: " + prand.nextFloat());
       System.out.println("Float: " + prand.nextFloat());
       System.out.println("Float: " + prand.nextFloat());
       System.out.println("Float: " + prand.nextFloat());
      
       System.out.println("Double: " + prand.nextDouble());
       System.out.println("Double: " + prand.nextDouble());
       System.out.println("Double: " + prand.nextDouble());
       System.out.println("Double: " + prand.nextDouble());
       System.out.println("Double: " + prand.nextDouble());
      
       System.out.println("Boolean: " + prand.nextBoolean());
       System.out.println("Boolean: " + prand.nextBoolean());
       System.out.println("Boolean: " + prand.nextBoolean());
       System.out.println("Boolean: " + prand.nextBoolean());
       System.out.println("Boolean: " + prand.nextBoolean());
   }
}

Output Screenshot:-

Note: Could you please consider my effort on this work and give up vote. Thank you :)

Add a comment
Know the answer?
Add Answer to:
Generating pseudo-random numbers. Develop a data type for generating pseudo-random numbers. That is, convert StdRandom to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This homework is an approach to generating random numbers. In this technique a seed value is...

    This homework is an approach to generating random numbers. In this technique a seed value is used to construct a new (seemingly random) value in the following manner:  The seed, s, is written down in n-bit binary.  Several bit positions, called taps, are used to generate a feedback bit. Bit n-1 is always one of the taps.  The feedback bit, f is the exclusive-or of the tap bit values  The seed is shifted to the left...

  • ​Declare an array with 1000 elements of type int Then in a loop generate 1000 random numbers and assign one to each element in the array The random numbers should be between 1 and 50 Then, prompt the user to enter a number, store this number in a local

    Rules to follow are:Declare an array with 1000 elements of type intThen in a loop generate 1000 random numbers and assign one to each element in the arrayThe random numbers should be between 1 and 50do not use rand or srand, use code is providedThen, prompt the user to enter a number, store this number in a local variable, the number should be safety checked using the GetInteger() functionThen, iterate through the array and determine how many times the user's...

  • Write a program in c++ that generates a 100 random numbers between 1 and 1000 and...

    Write a program in c++ that generates a 100 random numbers between 1 and 1000 and writes them to a data file called "randNum.dat". Then re-open the file, read the 100 numbers, print them to the screen, and find and print the minimum and maximum values. Specifications: If your output or input file fails to open correctly, print an error message that either states: Output file failed to open Input file failed to open depending on whether the output or...

  • In this question you will use the data type “bit32” introduced in Lab 2 (i.e. an...

    In this question you will use the data type “bit32” introduced in Lab 2 (i.e. an unsigned int). We will use the IEEE 754 standard for floating point numbers, with 1 sign bit, 8 bits for the exponent, and 23 bits for the mantissa. For the following programming questions, place each function in a common file “fplib.c” for compiling and testing. You are not to use any C floating point data types (i.e., float, double). You can use the bitwise...

  • Define a class named COMPLEX for complex numbers, which has two private data members of type...

    Define a class named COMPLEX for complex numbers, which has two private data members of type double (named real and imaginary) and the following public methods: 1- A default constructor which initializes the data members real and imaginary to zeros. 2- A constructor which takes two parameters of type double for initializing the data members real and imaginary. 3- A function "set" which takes two parameters of type double for changing the values of the data members real and imaginary....

  • guys can you please help me to to write a pseudo code for this program and...

    guys can you please help me to to write a pseudo code for this program and write the code of the program in visual studio in.cpp. compile the program and run and take screenshot of the output and upload it. please help is really appreciated. UTF-8"CPP Instruction SU2019 LA X 119SU-COSC-1436- C Get Homework Help With Che X Facebook -1.amazonaws.com/blackboard.learn.xythos.prod/584b1d8497c84/98796290? response-content-dis 100% School of Engineering and Technology COSC1436-LAB1 Note: in the instruction of the lab change "yourLastName" to your last...

  • DESCRIPTION: The range of integers that can be represented in Java using a primitive data type is...

    DESCRIPTION: The range of integers that can be represented in Java using a primitive data type is only from-263 to 263-1. What if we need to manipulate integer values beyond this range? In this assignment you will write a HugeInteger class which is able to represent arbitrar- ily large integer numbers. This class must implement arithmetic operations on integers such as addition, subtraction, multiplication, division and comparison. You have to implement this class without using Java predefined classes, unless specified...

  • For this assignment, your job is to create a simple game called Opoly. The objectives of...

    For this assignment, your job is to create a simple game called Opoly. The objectives of this assignment are to: Break down a problem into smaller, easier problems. Write Java methods that call upon other methods to accomplish tasks. Use a seed value to generate random a sequence of random numbers. Learn the coding practice of writing methods that perform a specific task. Opoly works this way: The board is a circular track of variable length (the user determines the...

  • Need help with assignment This assignment involves simulating a lottery drawing. In this type of lottery...

    Need help with assignment This assignment involves simulating a lottery drawing. In this type of lottery game, the player picks a set of numbers. A random drawing of numbers is then made, and the player wins if his/her chosen numbers match the drawn numbers (disregarding the order of the numbers). More specifically, a player picks k distinct numbers between 1 and n (inclusive), as well as one bonus number between 1 and m (inclusive). "Distinct" means that none of the...

  • python Topics covered: Using variables e Arithmetic operators Printing output Manipulating string objects . Generating random...

    python Topics covered: Using variables e Arithmetic operators Printing output Manipulating string objects . Generating random numbers . Getting user input NOTE: Each of your files must include a docstring at the top of the file containing your name, username, ID number and a description of the program. When solving these questions you must only use content covered in lectures 1 to 6 Submit the files containing your exercises using the Assignment Dropbox https:/adb auckland.ac.nz/Homel QUESTION 1 (3 MARKS) A...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT