Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. /**
  2. * Rolls a pair of n-sided dices, x amount of times, and gives the probability for each sum.
  3. *
  4. * @author Ricky Mutschlechner
  5. * @version 11/03/2010
  6. */
  7.  
  8. import java.util.Random;
  9. import java.util.Scanner;
  10. public class DiceProbability
  11. {
  12. public static void main(String[] args)
  13. {
  14. //Declare and initialize variables and objects
  15. Scanner in = new Scanner(System.in);
  16. Random randNumber = new Random();
  17.  
  18. int match = 0; //Number of times sum of dice matches the current sum
  19. int die1, die2; //Random generated numbers
  20.  
  21.  
  22. //Input: ask user for number of rolls and number of sides on a die
  23.  
  24. System.out.println("How many rolls would you like? ");
  25. int rolls = in.nextInt();
  26.  
  27. //If amount of sides are to be chosen:
  28. System.out.println("How many sides would you like? ");
  29. int sides = in.nextInt();
  30.  
  31. //Print heading for output table
  32. System.out.println("Number of Rolls: " + rolls);
  33. System.out.println(" Sum of Dice Probability");
  34.  
  35.  
  36. //***************************************************************************************
  37. //Using nested loops, cycle through the possible sums of the dice.
  38. //Roll the dice the given number of times for each sum.
  39. //Count how many times the sum of the dice match the current sum being looked for.
  40. //***************************************************************************************
  41.  
  42. int randNum = 0;
  43.  
  44. for(int n = 2; n <= (2 * sides); n++)
  45. {
  46. System.out.print("\n" + n + "s");
  47. int counter = 0;
  48. for (int i = 0; i < rolls; i++)
  49. {
  50. if (randNumber.nextInt(sides + 1) + randNumber.nextInt(sides + 1) == n){
  51. counter++;
  52. }
  53. }
  54. System.out.print(" " + ((double)counter/rolls*100));
  55. }
  56.  
  57.  
  58. } //end main
  59. }//end class DiceProbability
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement