Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class Dice
- {
- public static void main( String[] args)
- {
- Random randomNumbers = new Random(); // Generates random numbers
- int[] array = new int[ 7 ]; // Declares the array
- //Roll the die 36,000 times
- for ( int roll = 1; roll <=1000; roll++ )
- ++array[ 1 + randomNumbers.nextInt ( 6 ) ];
- System.out.printf( "%s%10sn", "Face", "Frequency" );
- // outputs array values
- for ( int face = 1; face < array.length; face++ )
- System.out.printf( "%4d%10dn", face, array[ face ] );
- } // end main
- } // end class DiceRolling
- import java.util.Random;
- public class Dice
- {
- public static void main(String[] args)
- {
- }
- private int numberShowing;
- private Random randomNumber = new Random();
- public Dice() {
- roll();
- }
- public int roll() {
- numberShowing = randomDiceRoll(1,6);
- return numberShowing;
- }
- private int randomDiceRoll(int low, int high) {
- return randomNumber.nextInt(high - low + 1) + low;
- }
- public int getNumberShowing() {
- return numberShowing;
- }
- public String toString() {
- return "Dice is showing value "+numberShowing;
- }
- };
- public class Dice
- {
- private int numberShowing;
- int[] howManyTimes = new int[7];
- public void roll()
- {
- numberShowing = (int)(Math.random() * 7);
- howManyTimes[numberShowing]++;
- }
- public void showHowManyTimes()
- {
- for (int i = 1; i < howManyTimes.length; i++)
- System.out.println(i + " rolled " + howManyTimes[i] + " times");
- }
- public static void main(String[] args)
- {
- Dice die = new Dice();
- final int NUMBER_OF_ROLLS = 1000;
- for (int i = 0; i < NUMBER_OF_ROLLS; i++)
- die.roll();
- die.showHowManyTimes();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement