Java Program to Emulate N Dice Roller

This is the java program for emulating N dice roller. This can be achieved using random numbers. User can also select how many dice in a game.

Here is the source code of the Java Program to Emulate N Dice Roller. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a sample program to emulate n dice roller
  2. import java.util.Random;
  3. import java.util.Scanner;
  6. public class Emulation_N_Dice 
  7. {
  8.     public static void main(String args[])
  9.     {
  10.         System.out.println("Enter the number of dice: ");
  11.         Scanner sc = new Scanner(;
  12.         int n = sc.nextInt();
  13.         Random rand = new Random();
  14.         do 
  15.         {
  16.             System.out.println("The values on dice are: ");
  17.             for(int i=0; i<n; i++)
  18.                 System.out.println(rand.nextInt(6)+1);
  20.             System.out.println("Continue: true/false");
  21.         } while (sc.nextBoolean() == true);
  22.         sc.close();
  23.     }
  24. }


$ javac
$ java Emulate_N_Dice
Enter the number of dice: 
The values on dice are: 
1 1 1 2 2 6
Continue: true/false
The values on dice are: 
6 5 2 5 5 1
Continue: true/false

Sanfoundry Global Education & Learning Series – 1000 Java Programs.


Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.