Advertisement
makispaiktis

Counting the numbers between an interval [E{X}-σ, Ε{X}+σ]

Apr 2nd, 2020 (edited)
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Random;
  3.  
  4. public class Stats {
  5.  
  6.     // Variables
  7.     static int average = 10;
  8.     static int stdDeviation = 1;
  9.     static int size = 1000000;
  10.    
  11.     // Constructor
  12.     Stats(){
  13.         average = 0;
  14.         stdDeviation = 1;
  15.     }
  16.    
  17.     // MAIN FUNCTION
  18.     public static void main(String[] args) {
  19.        
  20.         Random random = new Random();
  21.         ArrayList <Double> list = new ArrayList <Double> ();
  22.         for(int i=0; i<size; i++) {
  23.             double r = random.nextGaussian() * stdDeviation + average;
  24.             list.add(r);
  25.         }
  26.         // Now, my list is completed with random numbers
  27.         double sum = list.get(0);
  28.         double max = list.get(0);
  29.         double min = list.get(0);
  30.         for(int i=1; i<list.size(); i++) {
  31.             sum += list.get(i);
  32.             if(list.get(i) > max) {
  33.                 max = list.get(i);
  34.             }
  35.             if(list.get(i) < min) {
  36.                 min = list.get(i);
  37.             }
  38.         }
  39.         // Now, I will count how many numbers are in the range [9, 11] = [average - deviation, average + deviation]
  40.         int sum2 = 0;
  41.         for(int i=0; i<list.size(); i++) {
  42.             if((list.get(i) >= average - stdDeviation) && (list.get(i) <= average + stdDeviation)) {
  43.                 sum2++;
  44.             }
  45.         }
  46.         // Display the statistics of my survey
  47.         System.out.println("I will generate " + size + " random numbers, that follow Gaussian ");
  48.         System.out.println("distribution with average = " + average + " and stdDeviation = " + stdDeviation + ".");
  49.         System.out.println();
  50.         System.out.println("* Max number generated: " + max);
  51.         System.out.println("* Min number generated: " + min);
  52.         System.out.print("* There are " + sum2 + " numbers out of " + size + ", that are in the range [" + (average-stdDeviation));
  53.         System.out.println(", " + (average+stdDeviation) + "].");
  54.         double percentage = (double)sum2 / (double)size;
  55.         System.out.println("This means that " + (100*percentage) + "% of the generated numbers exist in the interval: ");
  56.         System.out.println("[E{X} - σ, E{x} + σ] = [average - stdDeviation, average + stdDeviation]");
  57.         System.out.println();
  58.        
  59.     }   // END OF MAIN FUNCTION
  60.    
  61. }   // END OF CLASS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement