Question
This program will use the Random class to generate random integers between some minimum (inclusive) and some maximum (inclusive) value. (1) Copy the following method
This program will use the Random class to generate random integers between some minimum (inclusive) and some maximum (inclusive) value.
(1) Copy the following method stub into your RandomGenerator class and complete it according to the specifications described in the header comments.
/** * Generates a random number using rand between min and max * inclusive. * * @param rand The Random object * @param min The minimum random value * @param max The maximum random value * @return A random number between min and max inclusive. */ public static int randomInt(Random rand, int min, int max) { //FILL IN BODY }
(2) Using the comments in the template as a guide, complete the main method.
You should create an instance of a Random class (Random object) with a seed of Config.SEED. The instance will be passed to your method randomInt as the argument for the first parameter.
Then, print out (each on their own line) the first 3 random integers generated by calling randomInt with the Random object previously created, a min of 1 and max of 6.
Finally, print out two additional calls to randomInt (each on their own line) with different parameters. Try to think of parameters that might test the functionality of your randomInt method in different ways.
The output for the first three calls should be:
3 5 6
For testing purposes, we require being able to predict the 'random' results. Setting a seed for the random number generator accomplishes this. In your program, set the seed for the instance (object) of the Random class to Config.SEED. Config.SEED is a constant named SEED declared in the Config.java file. Don't use the number in your RandomGenerator program, just use the constant.
CODE GIVEN:
Config.java:
//The SEED constant is used within RandomGenerator
//so that the 'randomly' generated numbers
//match the tests. Of course, if you really want
//randomly generated numbers, don't set the seed
//in the random number generator to a fixed number.
public class Config {
public static final int SEED = 456;
}
RandomGenerator.java
import java.util.Random;
public class RandomGenerator {
/**
* Generates a random number using rand between min and max
* inclusive.
*
* @param rand The Random object
* @param min The minimum random value
* @param max The maximum random value
* @return A random number between min and max inclusive.
*/
public static int randomInt(Random rand, int min, int max) {
//FILL IN BODY
}
public static void main(String[] args) {
// Create a Random object using Config.SEED as its random seed
/**
* Test your method by using print statements.
* Print out the first 3 pseudorandom integers generated with a min of 1
* and a max of 6. Each integer should be pringted on its own line.
*/
// Additional tests: ***DO NOT REMOVE THIS LINE***
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started