- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Roll a six-sided die 6000 times in Java
In order to roll a six sided die 6000 times in Java, we need to the nextInt() statement with decision making statements.
The nextInt() method returns the next random integer value from this random number generator sequence.
Declaration − The java.util.Random.nextInt() method is declared as follows −
public int nextInt()
Let us see a program to roll a six sided die 6000 times −
Example
import java.util.Random; public class Example { public static void main(String args[]) { Random rd = new Random(); // random number generator int freq[] = new int[6]; // creating an array to compute frequency of each face int val; int chance = 1; // rolling the dice 6000 times while(chance <= 6000){ val = 1 + rd.nextInt(6); // generates integers from 1 to 6 switch (val) { case 1: ++freq[0]; break; case 2: ++freq[1]; break; case 3: ++freq[2]; break; case 4: ++freq[3]; break; case 5: ++freq[4]; break; case 6: ++freq[5]; break; } chance++; } for(int i = 1; i <= 6; i++){ System.out.println("Side: " + i + "-> Frequency : " + freq[i - 1]); } } }
Output
Side: 1-> Frequency: 987 Side: 2-> Frequency : 971 Side: 3-> Frequency : 1057 Side: 4-> Frequency : 979 Side: 5-> Frequency : 982 Side: 6-> Frequency : 1024
- Related Articles
- A child has a die whose six faces show the letters as given below:The die is thrown once. What is the probability of getting (i) A?(ii) D?"
- Rolltop: A Laptop you can roll
- Dice Roll Simulation in C++
- Write the given statement in an equation form:Two times a number 'n' increased by six.
- Roll In Animation Effect with CSS
- Apothem of a n-sided regular polygon in C++
- How to find critical value for one-sided and two-sided t test in R?
- Laplace transform and Region of Convergence for right-sided and left-sided signals
- die() function in PHP
- Roll Out Animation Effect with CSS
- Python Pandas CustomBusinessHour - Roll provided date backward
- Roll the specified axis backwards until it lies in a given position in Numpy
- Can a person die due to Anaemia?
- The unless & die Function in Perl
- Bunty is playing a game with a regular die. If the number that turns up is even, he will gain four times the number that came up. If it is odd, he will lose 10 times the number that comes up. Bunty tosses the die 10 times in the game. What will be his final score? He tosses: 6,3,1,5,4,2,6,3,2,4 in the ten throws.

Advertisements