Question

In this problem you'll get to use one of Java's random number generators, java.util.Random (see the...

In this problem you'll get to use one of Java's random number generators, java.util.Random (see the docs for Random). This class generates a random double precision number uniformly distributed in the range [0.0 ... 1.0). That is, the numbers go from 0 to 1, including 0 but excluding 1. It is a common need in Java programming to generate a random integer number that's uniformly distributed in the range of [0 ... n), such as we saw in the dice rolling question a few weeks ago for n=6. The code to do this looks like:

double randomDouble = n * randomGenerator.nextDouble();

Do the following steps:

Create a class FillRandom.

Do your work in the fillRandom() method.

Prompt the user with "Enter a positive integer:".

Set up a Scanner to read from the keyboard and take in a positive integer randomSize from the user.

Allocate a double array whose length is 1,000,000.

Loop over the elements of the array, assigning array[k] the next double random number between 0 and randomSize. You will have to scale up the java.util.Random.nextRandom() value, which is in the range [0...1). You want it to be in the range [0...randomSize). So you must multiply it by randomSize, as shown in code above.

Now loop over the array again, computing the min, max and average of its values. You may want to use the Math.min() and Math.max() methods from java.lang.Math. (SAVE YOUR CODE before clicking on this link)

Print out the min value with "Min = " and the minimum

Print out the max value with "Max = " and the maximum

Print out the average with "Average = " and the average

Notice that the minimum over many random numbers in [0...n) is close to zero, the max is close to n, and the average is close to n/2.

In order to pass tests, use the following format:

Enter a positive integer:
Min     = <<your min>>
Max     = <<your max>>
Average = <<your average>>




// TODO: Add necessary import statements for Scanner and Random.

// Class to do array assignment and reading.
public class FillRandom {
  private static final int constArraySize = 1000000;
  
  public static void fillRandom(Random randomGenerator) {
    System.out.println("Enter a positive integer:");
    // TODO: Read a positive integer from the keyboard.
    // TODO: Allocate an array of constArraySize doubles.
    // TODO: Fill array[k] with random numbers in [0...randomSize).
    // TODO: Loop over the array again, computing min, max and average.
    // Print out the values. (Hint: these statements make use of variables you should declare above.)
    System.out.println("Min     = " + min);
    System.out.println("Max     = " + max);
    System.out.println("Average = " + average);
  }
  
  public static void main(String[] args) {
    // DO NOT MODIFY the main() method.
    Random randomGenerator;
    int seed = 314159265;
    if (args.length > 0) {
      seed = Integer.parseInt(args[0]);
    }
    randomGenerator = new Random(seed);
    
    // Call the student's function to do the rest of the work.
    fillRandom(randomGenerator);
  }
}
0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
In this problem you'll get to use one of Java's random number generators, java.util.Random (see the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • I need help asking the user to half the value of the displayed random number as...

    I need help asking the user to half the value of the displayed random number as well as storing each number generated by the program into another array list and displayed after the game is over at the end java.util.*; public class TestCode { public static void main(String[] args) { String choice = "Yes"; Random random = new Random(); Scanner scanner = new Scanner(System.in);    ArrayList<Integer> data = new ArrayList<Integer>(); int count = 0; while (!choice.equals("No")) { int randomInt =...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • Follow the directions in the TODO statements of RandomGenerator.java (attached). Submit your altered RandomGenerator.java file. public...

    Follow the directions in the TODO statements of RandomGenerator.java (attached). Submit your altered RandomGenerator.java file. public class RandomGenerator { public static void main(String[] args) { //TODO: Create a Scanner object to read keyboard input. //TODO: Ask the user a lower and upper bound. //TODO: Ask the user for the number of random integers to generate. //TODO: Generate random integers of the desired length and bound. //TODO: Print the average, max, min, count, and sum of the integers. } } 1....

  • Write four overloaded methods called randomize. Each method will return a random number based on the...

    Write four overloaded methods called randomize. Each method will return a random number based on the parameters that it receives: randomize() - Returns a random int between min and max inclusive. Must have two int parameters. randomize() - Returns a random int between 0 and max inclusive. Must have one int parameter. randomize() - Returns a random double between min and max. Must have two double parameters. randomize() - Returns a random double between 0 and max. Must have one...

  • I need to create a code for this prompt: In this project we will build a...

    I need to create a code for this prompt: In this project we will build a generic UserInput class for getting keyboard input from the user. Implementation: The class UserInput is a 'Methods only' class, and all the methods should be declared static. Look at the TestScanner.java program at the bottom of this page that inputs a string, int and double. It shows you how to use Scanner class to get input from the keyboard. Write FOUR simple methods, one...

  • Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file...

    Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesn’t exist. You will process through the file skipping any text or real (double) numbers. You will print the max, min, sum, count, and average of the integers in the file. You will want to create test files that contain integers, doubles, and Strings. HINT: Use hasNextInt() method and while loop. You may also want to use Integer.MAX_VALUE and Integer.MIN_VALUE for the initialization of...

  • I need help making this work correctly. I'm trying to do an array but it is...

    I need help making this work correctly. I'm trying to do an array but it is drawing from a safeInput class that I am supposed to use from a previous lab. The safeInput class is located at the bottom of this question I'm stuck and it is not printing the output correctly. The three parts I think I am having most trouble with are in Bold below. Thanks in advance. Here are the parameters: Create a netbeans project called ArrayStuff...

  • //please help I can’t figure out how to print and can’t get the random numbers to...

    //please help I can’t figure out how to print and can’t get the random numbers to print. Please help, I have the prompt attached. Thanks import java.util.*; public class Test { /** Main method */ public static void main(String[] args) { double[][] matrix = getMatrix(); // Display the sum of each column for (int col = 0; col < matrix[0].length; col++) { System.out.println( "Sum " + col + " is " + sumColumn(matrix, col)); } } /** getMatrix initializes an...

  • I need to change the following code so that it results in the sample output below...

    I need to change the following code so that it results in the sample output below but also imports and utilizes the code from the GradeCalculator, MaxMin, and Student classes in the com.csc123 package. If you need to change anything in the any of the classes that's fine but there needs to be all 4 classes. I've included the sample input and what I've done so far: package lab03; import java.util.ArrayList; import java.util.Scanner; import com.csc241.*; public class Lab03 { public...

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
ADVERTISEMENT