Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Main {
  4.  
  5. private final static int NUM_PICKS = 1000000;
  6. private final static int LIST_SIZE = 100;
  7. private List<Integer> list;
  8. Random r;
  9. public int listSize;
  10.  
  11. public static void main(String[] args) {
  12. Main main = new Main();
  13. main.go();
  14. }
  15.  
  16. public void go() {
  17. for (int j = 2; j < LIST_SIZE; j++) {
  18. listSize = j;
  19. List<Integer> outcomes = new ArrayList<>(j);
  20. for (int i = 0; i < j; i++) {
  21. Integer outcome = runTest();
  22. outcomes.add(outcome);
  23. // System.out.println("Picks: " + outcome);
  24. }
  25. long sum = 0L;
  26. int max = 0;
  27. int min = Integer.MAX_VALUE;
  28. for (int i = 0; i < j; i++) {
  29. sum += outcomes.get(i);
  30. if (outcomes.get(i) < min) {
  31. min = outcomes.get(i);
  32. }
  33. if (outcomes.get(i) > max) {
  34. max = outcomes.get(i);
  35. }
  36. }
  37.  
  38. double mean = (double) sum / (double) j;
  39. Collections.sort(outcomes);
  40. int mostCounted = 0;
  41. int mostCountedValue = 0;
  42. int currentCounted = 0;
  43. int currentValue = 0;
  44. for (int i = 0;i<j;i++) {
  45. if (outcomes.get(i) == currentValue) {
  46. currentCounted++;
  47. } else {
  48. if (currentCounted > mostCounted) {
  49. mostCounted = currentCounted;
  50. mostCountedValue = currentValue;
  51. }
  52. currentValue= outcomes.get(i);
  53. currentCounted = 1;
  54. }
  55. }
  56. if (currentCounted > mostCounted) {
  57. mostCounted = currentCounted;
  58. mostCountedValue = currentValue;
  59. }
  60. int mode = mostCountedValue;
  61. int median = outcomes.get(j/2);
  62. System.out.println("RAN FOR N = " + j + " FOR " + NUM_PICKS + " ITERATIONS");
  63. System.out.println("Mean: " + mean);
  64. System.out.println("Median: " + median);
  65. System.out.println("Mode: " + mode);
  66.  
  67. }
  68. }
  69.  
  70. public int runTest() {
  71. r = new Random();
  72.  
  73. list = new ArrayList<Integer>(listSize);
  74. for (int i = 0; i < listSize; i++) {
  75. list.add(i);
  76. }
  77.  
  78. int numPicks = 0;
  79. while (!isAllSame()) {
  80. randomPick();
  81. numPicks++;
  82. }
  83. return numPicks;
  84. }
  85.  
  86. private void randomPick() {
  87. int pick = r.nextInt(listSize);
  88. Integer value = list.get(pick);
  89. for (int i = 0; i < listSize; i++) {
  90. int nextValue = list.get(i);
  91. list.set(i, value);
  92. value = nextValue;
  93. }
  94. }
  95.  
  96. private boolean isAllSame() {
  97. int firstValue = list.get(0);
  98. for (int i = 1; i < listSize; i++) {
  99. if (list.get(i) != firstValue) {
  100. return false;
  101. }
  102. }
  103. return true;
  104. }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement