Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Random;
- public class Stats {
- // Variables
- static int average = 10;
- static int stdDeviation = 1;
- static int size = 1000000;
- // Constructor
- Stats(){
- average = 0;
- stdDeviation = 1;
- }
- // MAIN FUNCTION
- public static void main(String[] args) {
- Random random = new Random();
- ArrayList <Double> list = new ArrayList <Double> ();
- for(int i=0; i<size; i++) {
- double r = random.nextGaussian() * stdDeviation + average;
- list.add(r);
- }
- // Now, my list is completed with random numbers
- double sum = list.get(0);
- double max = list.get(0);
- double min = list.get(0);
- for(int i=1; i<list.size(); i++) {
- sum += list.get(i);
- if(list.get(i) > max) {
- max = list.get(i);
- }
- if(list.get(i) < min) {
- min = list.get(i);
- }
- }
- // Now, I will count how many numbers are in the range [9, 11] = [average - deviation, average + deviation]
- int sum2 = 0;
- for(int i=0; i<list.size(); i++) {
- if((list.get(i) >= average - stdDeviation) && (list.get(i) <= average + stdDeviation)) {
- sum2++;
- }
- }
- // Display the statistics of my survey
- System.out.println("I will generate " + size + " random numbers, that follow Gaussian ");
- System.out.println("distribution with average = " + average + " and stdDeviation = " + stdDeviation + ".");
- System.out.println();
- System.out.println("* Max number generated: " + max);
- System.out.println("* Min number generated: " + min);
- System.out.print("* There are " + sum2 + " numbers out of " + size + ", that are in the range [" + (average-stdDeviation));
- System.out.println(", " + (average+stdDeviation) + "].");
- double percentage = (double)sum2 / (double)size;
- System.out.println("This means that " + (100*percentage) + "% of the generated numbers exist in the interval: ");
- System.out.println("[E{X} - σ, E{x} + σ] = [average - stdDeviation, average + stdDeviation]");
- System.out.println();
- } // END OF MAIN FUNCTION
- } // END OF CLASS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement