Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class Dice
  4. {
  5. public static void main( String[] args)
  6. {
  7. Random randomNumbers = new Random(); // Generates random numbers
  8. int[] array = new int[ 7 ]; // Declares the array
  9.  
  10. //Roll the die 36,000 times
  11. for ( int roll = 1; roll <=1000; roll++ )
  12. ++array[ 1 + randomNumbers.nextInt ( 6 ) ];
  13.  
  14. System.out.printf( "%s%10sn", "Face", "Frequency" );
  15.  
  16. // outputs array values
  17. for ( int face = 1; face < array.length; face++ )
  18. System.out.printf( "%4d%10dn", face, array[ face ] );
  19. } // end main
  20. } // end class DiceRolling
  21.  
  22. import java.util.Random;
  23.  
  24. public class Dice
  25. {
  26. public static void main(String[] args)
  27. {
  28.  
  29. }
  30. private int numberShowing;
  31. private Random randomNumber = new Random();
  32.  
  33. public Dice() {
  34. roll();
  35. }
  36.  
  37. public int roll() {
  38. numberShowing = randomDiceRoll(1,6);
  39. return numberShowing;
  40. }
  41.  
  42. private int randomDiceRoll(int low, int high) {
  43. return randomNumber.nextInt(high - low + 1) + low;
  44. }
  45.  
  46. public int getNumberShowing() {
  47. return numberShowing;
  48. }
  49.  
  50. public String toString() {
  51. return "Dice is showing value "+numberShowing;
  52. }
  53. };
  54.  
  55. public class Dice
  56. {
  57. private int numberShowing;
  58. int[] howManyTimes = new int[7];
  59.  
  60. public void roll()
  61. {
  62. numberShowing = (int)(Math.random() * 7);
  63. howManyTimes[numberShowing]++;
  64. }
  65.  
  66. public void showHowManyTimes()
  67. {
  68. for (int i = 1; i < howManyTimes.length; i++)
  69. System.out.println(i + " rolled " + howManyTimes[i] + " times");
  70. }
  71.  
  72. public static void main(String[] args)
  73. {
  74.  
  75. Dice die = new Dice();
  76. final int NUMBER_OF_ROLLS = 1000;
  77.  
  78. for (int i = 0; i < NUMBER_OF_ROLLS; i++)
  79. die.roll();
  80.  
  81. die.showHowManyTimes();
  82.  
  83. }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement