Statistics is all about large groups of numbers.
When talking about a set of sampled data, most frequently used is their mean value and standard deviation (stddev).
If you have set of data 




x

i




{\displaystyle x_{i}}

 where 



i
=
1
,
2
,
…
,
n




{\displaystyle i=1,2,\ldots ,n\,\!}

, the mean is 






x
¯



≡


1
n



∑

i



x

i




{\displaystyle {\bar {x}}\equiv {1 \over n}\sum _{i}x_{i}}

, while the stddev is 



σ
≡




1
n



∑

i




(

x

i


−



x
¯



)


2






{\displaystyle \sigma \equiv {\sqrt {{1 \over n}\sum _{i}\left(x_{i}-{\bar {x}}\right)^{2}}}}

.

When examining a large quantity of data, one often uses a histogram, which shows the counts of data samples falling into a prechosen set of intervals (or bins).
When plotted, often as bar graphs, it visually indicates how often each data value occurs.

Task Using your language's random number routine, generate real numbers in the range of [0, 1]. It doesn't matter if you chose to use open or closed range.
Create 100 of such numbers (i.e. sample size 100) and calculate their mean and stddev.
Do so for sample size of 1,000 and 10,000, maybe even higher if you feel like.
Show a histogram of any of these sets.
Do you notice some patterns about the standard deviation?

Extra Sometimes so much data need to be processed that it's impossible to keep all of them at once. Can you calculate the mean, stddev and histogram of a trillion numbers? (You don't really need to do a trillion numbers, just show how it can be done.)

Hint

For a finite population with equal probabilities at all points, one can derive:








(
x
−


x
¯



)

2



¯


=



x

2


¯


−



x
¯



2




{\displaystyle {\overline {(x-{\overline {x}})^{2}}}={\overline {x^{2}}}-{\overline {x}}^{2}}


Or, more verbosely:







1
N



∑

i
=
1


N


(

x

i


−


x
¯



)

2


=


1
N



(

∑

i
=
1


N



x

i


2


)

−



x
¯



2


.


{\displaystyle {\frac {1}{N}}\sum _{i=1}^{N}(x_{i}-{\overline {x}})^{2}={\frac {1}{N}}\left(\sum _{i=1}^{N}x_{i}^{2}\right)-{\overline {x}}^{2}.}


See also
 Statistics/Normal distribution
Tasks for calculating statistical measures



in one go
moving (sliding window)
moving (cumulative)

Mean
Arithmetic

Statistics/Basic
Averages/Arithmetic mean
Averages/Pythagorean means


Averages/Simple moving average




Geometric

Averages/Pythagorean means




Harmonic

Averages/Pythagorean means




Quadratic

Averages/Root mean square




Circular

Averages/Mean angle
Averages/Mean time of day




Median

Averages/Median




Mode

Averages/Mode




Standard deviation

Statistics/Basic



Cumulative standard deviation






/* Import math library to get:
 *     	1) Square root function 	        : Math.sqrt(x)
 *	2) Power function 		: Math.pow(base, exponent)
 *	3) Random number generator 	: Math.Random()
 */		
import 'dart:math' as Math show sqrt, pow, Random;
 
// Returns average/mean of a list of numbers
num mean(List<num> l)  => l.reduce((num value,num element)=>value+element)/l.length;
 
// Returns standard deviation of a list of numbers
num stdev(List<num> l) => Math.sqrt((1/l.length)*l.map((num x)=>x*x).reduce((num value,num element) => value+element) - Math.pow(mean(l),2));
 
/* CODE TO PRINT THE HISTOGRAM STARTS HERE
 *
 * 	Histogram has ten fields, one for every tenth between 0 and 1
 * 	To do this, we save the histogram as a global variable
 * 	that will hold the number of occurences of each tenth in the sample
 */
List<num> histogram = new List.filled(10,0);
 
/*
 * METHOD TO CREATE A RANDOM SAMPLE OF n NUMBERS (Returns a list)
 *
 * 	While creating each value, this method also increments the
 * 	appropriate index of the histogram
 */
List<num> randomsample(num n){
  List<num> l = new List<num>(n);
  histogram = new List.filled(10,0);
  num random = new Math.Random();
  for (int i = 0; i < n; i++){
    l[i] = random.nextDouble();
    histogram[conv(l[i])] += 1; 
  }
  return l;
}
 
/*
 * METHOD TO RETURN A STRING OF n ASTERIXES (yay ASCII art)
 */
String stars(num n){
  String s = '';
  for (int i = 0; i < n; i++){
    s = s + '*';
  }
  return s;
}
 
/*
 * METHOD TO DRAW THE HISTOGRAM
 * 1) Get to total for all the values in the histogram
 * 2) For every field in the histogram:
 * 		a) Compute the frequency for every field in the histogram
 * 		b) Print the frequency as asterixes 
 */
void drawhistogram(){
  int total = histogram.reduce((num element,num value)=>element+value);
  double freq;
  for (int i = 0; i < 10; i++){
    freq = histogram[i]/total;
    print('${i/10} - ${(i+1)/10} : ' + stars(conv(30*freq)));
  }
}
 
/* HELPER METHOD: 
 * 	converts values between 0-1 to integers between 0-9 inclusive
 * 	useful to figure out which random value generated 
 *	corresponds to which field in the histogram
 */
int conv(num i) => (10*i).floor();
 
 
/* MAIN FUNCTION 
 *
 * Create 5 histograms and print the mean and standard deviation for each:
 * 	1) Sample Size = 100
 *	2) Sample Size = 1000
 *	3) Sample Size = 10000
 *	4) Sample Size = 100000
 *	5) Sample Size = 1000000
 *  
 */
void main(){
  List<num> l;
  num m;
  num s;
  List<int> sampleSizes = [100,1000,10000,100000,1000000];
  for (int samplesize in sampleSizes){
    print('---------------  Sample size $samplesize   ----------------');
    l = randomsample(samplesize);
    m = mean(l);
    s = stdev(l);
    drawhistogram();
    print('');
    print('mean: ${m.toStringAsPrecision(8)}   standard deviation: ${s.toStringAsPrecision(8)}');
    print('');
  }
}