Program to Emulate N Dice Roller


Suppose we have ‘N’ number of dice and we roll all of them at a time, then we need to show all the values that occurred on all dice. We have to emulate the same situation using Java programs. To solve this problem, we will use a class named ‘Random’ that comes under ‘java.util’ package.

Java program to Emulate N Dice Roller

Random Class

We create an object of this class to generate pseudorandom numbers within a given range. We will customize this object and apply our own logic to select any random values from the specified dice. To retrieve the values from dice we need a built-in method named ‘nextInt()’ that returns the next integer value from a specified sequence.

Following is the syntax to create an object of class Random −

Syntax

Random nameOfObject = new Random();

Example 1

In the following example, we will use the class ‘Random’ to emulate dice 1 time.

Approach

  • Create an object named ‘rndm’ of class ‘Random’.

  • Declare and initialize a variable named ‘noOfdice’ that signifies number of dice.

  • Take a for loop that will iterate through total number of dice and print the value on dice using ‘rndm’ object along with ‘nextInt()’ method.

  • We will use 6 as an argument of ‘nextInt()’ and add 1 to it because a dice has values 1 to 6. If we don’t add 1 it may print 0.

Code

import java.util.*;
public class Dice {
   public static void main(String[] args) {
      Random rndm = new Random();
      int noOfdice = 5;
      System.out.print("Occurrence of values on dice: ");
      for (int i = 0; i < noOfdice; i++) {
         System.out.print(rndm.nextInt(6) + 1);
         System.out.print(" ");
      }
   }
}

Output

Occurrence of values on dice: 6 2 1 5 1

Example 2

In the following example, we will use the class ‘Random’ to emulate dice 2 times.

Approach

  • We will change the above example program and set the value of ‘occurrence’ to 2 using while loop to print values 2 times.

Code

import java.util.*;
public class Dice {
   public static void main(String[] args) {
      Random rndm = new Random();
      int noOfdice = 5;
      int occurrence = 2;
      while(occurrence != 0) {
         System.out.print("Occurrence of values on dice: ");
         for (int i = 0; i < noOfdice; i++) {
            System.out.print(rndm.nextInt(6) + 1);
            System.out.print(" ");
         }
         System.out.println();
         occurrence--;
      }
   }
}

Output

Occurrence of values on dice: 4 6 2 3 2
Occurrence of values on dice: 2 6 3 2 4

Example 3

In the following example, we will use the class ‘Random’ to emulate dice n number of times.

Approach

  • Again, we will change the above example code. We will write our logic in do-while loop.

  • First, take number of dice from user. In the do block, iterate through total number of dice and prints the value of dice.

  • Then, we ask if user wants to continue or not. If user will enter 1, the whole process repeats itself and if user will enter 0, we exit the program.

Code

import java.util.*;
public class Dice2 {
   public static void main(String[] args) {
      Random rndm = new Random();
      System.out.println("Enter how many dice you have: ");
      Scanner sc = new Scanner(System.in);
      int noOfdice = sc.nextInt();
      do {
         System.out.print("Occurrence of values on dice: ");
         for (int i = 0; i < noOfdice; i++) {
            System.out.print(rndm.nextInt(6) + 1);
            System.out.print(" ");
         }
         System.out.println();
         System.out.println(" Press 1 to continue! 0 to exit! ");
      } while(sc.nextInt() == 1);
   }
}

Output

Enter how many dice you have:
3
Occurrence of values on dice: 5 2 3
Press 1 to continue! 0 to exit!
1
Occurrence of values on dice: 4 3 1
Press 1 to continue! 0 to exit!
0

Conclusion

In this article, we have discussed three example programs to emulate a dice roller ‘N’ number of times. We have defined a custom logic and applied it with the object of class ‘Random’ to select and retrieve items from dice. The Random class is available in ‘java.util’ package

Updated on: 20-Jul-2023

108 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements