In: Computer Science
The Random class implements a random number generator, which produces sequences of numbers that appear to be random. To generate random integers, you construct an object of the Random class, and then apply the nextInt method. For example, the call generator.nextInt(6) gives you a random number between 0 and 5. Write a program DieSimulator that uses the Random class to simulate the cast of a die, printing a random number between 1 and 6 every time that the program is ru
Hi,
Following is the code for generating random number between 1 to 6
uncomment the for loop if you want to generate more random numbers in one run.
Code
import java.util.Random;
public class DieSimulator
{
public static void main(String[] args)
{
//random class
Random rand = new Random();
//numbers between 1 to 6
int minRange = 1, maxRange= 6;
//random value
int value = rand.nextInt(maxRange - minRange) + minRange;
System.out.println("Random Number :" +value);
//genereating 10 random numbers in a for loop
/* for(int i=0;i<10;i++){
int value = rand.nextInt(maxRange - minRange) + minRange;
System.out.println("Random Number :"+value);
}*/
}
}
output