Jump to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Basic statistical methods

Featured Replies

mean

median

mode

range

package org.bjornkrols.math;

import java.util.*;

/**
 * Created by Bjorn on 5/05/2015.
 */
public final class Statistics {

    private Statistics() {
        // This class should never be instantiated.
        // Do not delete or make accessible.
    }

    /**
     * @return The average value.
     */
    public static double mean(int... n) {
        double total = 0;
        for (int i : n) total += i;
        return total / n.length;
    }

    /**
     * @return Midpoint between the lowest and highest value of the set.
     */
    public static double median(int... n) {
        Arrays.sort(n);
        int length = n.length;
        int center = n[length / 2];
        return length % 2 == 0 ? mean(n[(length / 2) - 1], center) : center;
    }

    /**
     * @return The most frequently occurring value(s) of the set.
     */
    public static List<Integer> mode(int... n) {
        Map<Integer, Integer> occurrence = new HashMap<Integer, Integer>(n.length);
        int maximum = 0;
        List<Integer> maximumElements = new ArrayList<Integer>();
        for (int i : n) {
            Integer o = occurrence.get(i);
            if (o == null) occurrence.put(i, o = 1);
            else occurrence.put(i, o += 1);
            if (o > maximum) {
                maximum = o;
                maximumElements.clear();
                maximumElements.add(i);
            } else if (o == maximum) maximumElements.add(i);
        }
        return maximumElements;
    }

    /**
     * @return The span of values over which the data set occurs.
     */
    public static int range(int... n) {
        int minimum = Integer.MAX_VALUE;
        int maximum = Integer.MIN_VALUE;
        for (int i : n) {
            if (i < minimum) minimum = i;
            if (i > maximum) maximum = i;
        }
        return maximum - minimum;
    }

}

Edited by Botre

Store occurance.get(i) in a variable to avoid excess accesses to the map. Same with length/2, to lower the overhead from calculations

The mode method is pretty expensive. Try something a little less intense:

int mode(int...numbers) {
    int highestNumber = 0, mostOccurrences = 0;
    int[] counter = new int[numbers.length];

    for(int i = 0; i < counter.length; i++) {
        int currentNumber = numbers[i];
        int occurrences = counter[currentNumber]++;

        if(mostOccurrences < occurrences) {
            mostOccurrences = occurrences;
            highestNumber = currentNumber;
        }
    }

    return highestNumber;
}
  • Author

 

Store occurance.get(i) in a variable to avoid excess accesses to the map. Same with length/2, to lower the overhead from calculations

The mode method is pretty expensive. Try something a little less intense:

int mode(int...numbers) {
    int highestNumber = 0, mostOccurrences = 0;
    int[] counter = new int[numbers.length];

    for(int i = 0; i < counter.length; i++) {
        int currentNumber = numbers[i];
        int occurrences = counter[currentNumber]++;

        if(mostOccurrences < occurrences) {
            mostOccurrences = occurrences;
            highestNumber = currentNumber;
        }
    }

    return highestNumber;
}

 

Thanks for the feedback!

(updated)

Edited by Botre

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.