The Random class in java can be very helpful for performing simulations and creating games. A basic way to use this class is to simulate a dice, which means getting a random number from a certain range depending on how many sides the dice contains. It’s fairly easy and quick to make, which means anyone with a decent knowledge of java could easily make it.
Steps
-
Open your IDE application on your computer.
-
Create a new project. Name it DiceSimulator. If it makes a main class automatically, call that class DiceTester.Advertisement
-
Create a new class and name it Dice.
- In this Dice file, import the random package:
import java.util.Random;
- In this Dice file, import the random package:
-
Within the Dice class, initiate a random variable:
-
Random randomGenerator = new Random();
-
-
Initiate an integer variable to indicate the number of sides:
-
int sides = 0;
-
-
Create the constructor for Dice to define the number of sides the dice class would have:
-
public Dice (int numberOfSides) { sides = numberOfSides;}
-
-
Create a method to return a random number between 1 and the number of sides:
-
public int roll() {int result = randomGenerator.nextInt(sides) + 1; return result; }
-
-
Create the main class and name it DiceTester.
- If DiceTester is your main class, go straight to DiceTester instead.
-
At the top of the DiceTester class, import the scanner package:
-
import java.util.Scanner;
-
-
Create a Scanner object in the main method and name it in.
-
Print out the question: “How many dice do you need?”
- If you're new to programming, use
System.out.println(" ");
to print statements.
- If you're new to programming, use
-
Initiate an integer variable called howManyDice and assign it to the integer the user inputs:
-
int howManyDice = in.nextInt();
-
-
Print out the question: “How many sides do each dice have?”
-
Initiate an integer variable called howManySides and assign it to the integer the user inputs:
-
int howManySides = in.nextInt();
-
-
Create a for loop that iterates once for every dice the user wishes to create.
- Within this loop, you construct each Dice object by using the for loop variable x and passing the variable howManySides.
-
Call the roll method from Dice and display it in the loop to get all the results.
-
Check that the loop looks similar to the following code:
-
for (int x = 0; x < howManyDice; x++) {theDice[x] = new Dice(howManySides); int result = theDice[x].roll(); System.out.println("Roll of dice #" + (1 + x) + ": " + result); }
-
-
Run the program!
- In many IDE its by pressing the green play button on the top left corner of your IDE application.
Advertisement
Expert Q&A
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement
Tips
- Try to compile and run the program as you put new code in to find any errors in your program!Thanks
- Keep your code organized so you can easily find certain areas of your code for later review.Thanks
- Leave comments using // to leave information where you believe will need extra attention or for other programmers to look over your program!Thanks
Advertisement
About this article
Thanks to all authors for creating a page that has been read 17,509 times.
Advertisement