fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.text.DecimalFormat;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. Scanner cin = new Scanner(System.in);
  14. boolean run = true ;
  15. int[] sum = new int[13] ;
  16. float[] percent = new float[13];
  17. int streak = 0, streakValue = 0, currentStreak = 0, streakStart = 0, trials = 0 ;
  18. int dice1 = 0, dice2 = 0 ;
  19. for(int i = 0; i < sum.length; ++i)
  20. {
  21. sum[i] = 0 ;
  22. percent[i] = 0.0f;
  23. }
  24. do
  25. {
  26. trials = cin.nextInt();
  27. int prevSum = 0 ;
  28.  
  29. for(int t = 0; t < trials; ++t)
  30. {
  31. dice1 = (int) ((Math.random() * 6) + 1);
  32. dice2 = (int) ((Math.random() * 6) + 1);
  33. sum[dice1 + dice2]++ ;
  34. if((dice1 + dice2) == prevSum)
  35. {
  36. ++currentStreak ;
  37. streakValue = prevSum ;
  38. if(currentStreak > streak)
  39. {
  40. streak = currentStreak ;
  41. streakStart = t - streak ;
  42. }
  43. }
  44. else {
  45. currentStreak = 1 ;
  46. }
  47. prevSum = dice1 + dice2 ;
  48. }
  49.  
  50. System.out.println("Continue ? (y/n) : ");
  51. run = cin.nextLine().equals("y");
  52. } while(run);
  53.  
  54. DecimalFormat df = new DecimalFormat("###.##");
  55.  
  56. // Start from 2 - the minimum sum possible.
  57. for(int i = 2; i < 13; ++i)
  58. {
  59. percent[i] = (float)sum[i] / (float)trials * 100.0f ;
  60. System.out.println("The sum " + i + " has occurred " + df.format(percent[i]) + "% of times");
  61. }
  62. System.out.println("Longest streak of " + streakValue + " has occurred for " + streak + " times");
  63. System.out.println("It started at : " + streakStart);
  64. }
  65.  
  66. }
Success #stdin #stdout 0.14s 321088KB
stdin
50
n
stdout
Continue ? (y/n) : 
The sum 2 has occurred 0% of times
The sum 3 has occurred 8% of times
The sum 4 has occurred 12% of times
The sum 5 has occurred 12% of times
The sum 6 has occurred 10% of times
The sum 7 has occurred 16% of times
The sum 8 has occurred 10% of times
The sum 9 has occurred 14% of times
The sum 10 has occurred 4% of times
The sum 11 has occurred 10% of times
The sum 12 has occurred 4% of times
Longest streak of 3 has occurred for 2 times
It started at : 32