Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Main {
- private final static int NUM_PICKS = 1000000;
- private final static int LIST_SIZE = 100;
- private List<Integer> list;
- Random r;
- public int listSize;
- public static void main(String[] args) {
- Main main = new Main();
- main.go();
- }
- public void go() {
- for (int j = 2; j < LIST_SIZE; j++) {
- listSize = j;
- List<Integer> outcomes = new ArrayList<>(j);
- for (int i = 0; i < j; i++) {
- Integer outcome = runTest();
- outcomes.add(outcome);
- // System.out.println("Picks: " + outcome);
- }
- long sum = 0L;
- int max = 0;
- int min = Integer.MAX_VALUE;
- for (int i = 0; i < j; i++) {
- sum += outcomes.get(i);
- if (outcomes.get(i) < min) {
- min = outcomes.get(i);
- }
- if (outcomes.get(i) > max) {
- max = outcomes.get(i);
- }
- }
- double mean = (double) sum / (double) j;
- Collections.sort(outcomes);
- int mostCounted = 0;
- int mostCountedValue = 0;
- int currentCounted = 0;
- int currentValue = 0;
- for (int i = 0;i<j;i++) {
- if (outcomes.get(i) == currentValue) {
- currentCounted++;
- } else {
- if (currentCounted > mostCounted) {
- mostCounted = currentCounted;
- mostCountedValue = currentValue;
- }
- currentValue= outcomes.get(i);
- currentCounted = 1;
- }
- }
- if (currentCounted > mostCounted) {
- mostCounted = currentCounted;
- mostCountedValue = currentValue;
- }
- int mode = mostCountedValue;
- int median = outcomes.get(j/2);
- System.out.println("RAN FOR N = " + j + " FOR " + NUM_PICKS + " ITERATIONS");
- System.out.println("Mean: " + mean);
- System.out.println("Median: " + median);
- System.out.println("Mode: " + mode);
- }
- }
- public int runTest() {
- r = new Random();
- list = new ArrayList<Integer>(listSize);
- for (int i = 0; i < listSize; i++) {
- list.add(i);
- }
- int numPicks = 0;
- while (!isAllSame()) {
- randomPick();
- numPicks++;
- }
- return numPicks;
- }
- private void randomPick() {
- int pick = r.nextInt(listSize);
- Integer value = list.get(pick);
- for (int i = 0; i < listSize; i++) {
- int nextValue = list.get(i);
- list.set(i, value);
- value = nextValue;
- }
- }
- private boolean isAllSame() {
- int firstValue = list.get(0);
- for (int i = 1; i < listSize; i++) {
- if (list.get(i) != firstValue) {
- return false;
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement