Statistics/Normal distribution: Difference between revisions

No edit summary
Line 13:
 
=={{header|C}}==
<lang C>/*
* RosettaCode example: Statistics/Normal distribution in C
*
* The random number generator rand() of the standard C library is obsolete
* and should not be used in more demanding applications. There are plenty
* libraries with advanced features (eg. GSL) with functions to calculate
* the mean, the standard deviation, generating random numbers etc.
* However, these features are not the core of the standard C library.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>
 
 
#define NMAX 10000000
 
 
double mean(double* values, int n)
{
int i;
double s = 0;
 
for ( i = 0; i < n; i++ )
s += values[i];
return s / n;
}
 
 
double stddev(double* values, int n)
{
int i;
double average = mean(values,n);
double s = 0;
 
for ( i = 0; i < n; i++ )
s += (values[i] - average) * (values[i] - average);
return sqrt(s / (n - 1));
}
 
/*
* Normal random numbers generator - Marsaglia algorithm.
*/
double* generate(int n)
{
int i;
int m = n + n % 2;
double* values = (double*)calloc(m,sizeof(double));
double average, deviation;
 
if ( values )
{
for ( i = 0; i < m; i += 2 )
{
double x,y,rsq,f;
do {
x = 2.0 * rand() / (double)RAND_MAX - 1.0;
y = 2.0 * rand() / (double)RAND_MAX - 1.0;
rsq = x * x + y * y;
}while( rsq >= 1. || rsq == 0. );
f = sqrt( -2.0 * log(rsq) / rsq );
values[i] = x * f;
values[i+1] = y * f;
}
}
return values;
}
 
 
void printHistogram(double* values, int n)
{
const int width = 50;
int max = 0;
 
const double low = -3.05;
const double high = 3.05;
const double delta = 0.1;
 
int i,j,k;
int nbins = (int)((high - low) / delta);
int* bins = (int*)calloc(nbins,sizeof(int));
if ( bins != NULL )
{
for ( i = 0; i < n; i++ )
{
int j = (int)( (values[i] - low) / delta );
if ( 0 <= j && j < nbins )
bins[j]++;
}
 
for ( j = 0; j < nbins; j++ )
if ( max < bins[j] )
max = bins[j];
 
for ( j = 0; j < nbins; j++ )
{
printf("(%5.2f, %5.2f) |", low + j * delta, low + (j + 1) * delta );
k = (int)( (double)width * (double)bins[j] / (double)max );
while(k-- > 0) putchar('*');
printf(" %-.1f%%", bins[j] * 100.0 / (double)n);
putchar('\n');
}
 
free(bins);
}
}
 
 
int main(void)
{
double* seq;
 
srand((unsigned int)time(NULL));
 
if ( (seq = generate(NMAX)) != NULL )
{
printf("mean = %g, stddev = %g\n\n", mean(seq,NMAX), stddev(seq,NMAX));
printHistogram(seq,NMAX);
free(seq);
 
printf("\n%s\n", "press enter");
getchar();
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}</lang>
{{out}}
<pre>mean = 0.000477941, stddev = 0.999945
 
(-3.05, -2.95) | 0.1%
(-2.95, -2.85) | 0.1%
(-2.85, -2.75) |* 0.1%
(-2.75, -2.65) |* 0.1%
(-2.65, -2.55) |* 0.1%
(-2.55, -2.45) |** 0.2%
(-2.45, -2.35) |** 0.2%
(-2.35, -2.25) |*** 0.3%
(-2.25, -2.15) |**** 0.4%
(-2.15, -2.05) |***** 0.4%
(-2.05, -1.95) |****** 0.5%
(-1.95, -1.85) |******** 0.7%
(-1.85, -1.75) |********* 0.8%
(-1.75, -1.65) |*********** 0.9%
(-1.65, -1.55) |************* 1.1%
(-1.55, -1.45) |**************** 1.3%
(-1.45, -1.35) |****************** 1.5%
(-1.35, -1.25) |********************* 1.7%
(-1.25, -1.15) |************************ 1.9%
(-1.15, -1.05) |*************************** 2.2%
(-1.05, -0.95) |****************************** 2.4%
(-0.95, -0.85) |********************************* 2.7%
(-0.85, -0.75) |************************************ 2.9%
(-0.75, -0.65) |*************************************** 3.1%
(-0.65, -0.55) |***************************************** 3.3%
(-0.55, -0.45) |******************************************** 3.5%
(-0.45, -0.35) |********************************************** 3.7%
(-0.35, -0.25) |*********************************************** 3.8%
(-0.25, -0.15) |************************************************* 3.9%
(-0.15, -0.05) |************************************************* 4.0%
(-0.05, 0.05) |************************************************** 4.0%
( 0.05, 0.15) |************************************************* 4.0%
( 0.15, 0.25) |************************************************* 3.9%
( 0.25, 0.35) |*********************************************** 3.8%
( 0.35, 0.45) |********************************************** 3.7%
( 0.45, 0.55) |******************************************** 3.5%
( 0.55, 0.65) |***************************************** 3.3%
( 0.65, 0.75) |*************************************** 3.1%
( 0.75, 0.85) |************************************ 2.9%
( 0.85, 0.95) |********************************* 2.7%
( 0.95, 1.05) |****************************** 2.4%
( 1.05, 1.15) |*************************** 2.2%
( 1.15, 1.25) |************************ 1.9%
( 1.25, 1.35) |********************* 1.7%
( 1.35, 1.45) |****************** 1.5%
( 1.45, 1.55) |**************** 1.3%
( 1.55, 1.65) |************* 1.1%
( 1.65, 1.75) |*********** 0.9%
( 1.75, 1.85) |********* 0.8%
( 1.85, 1.95) |******** 0.7%
( 1.95, 2.05) |****** 0.5%
( 2.05, 2.15) |***** 0.4%
( 2.15, 2.25) |**** 0.4%
( 2.25, 2.35) |*** 0.3%
( 2.35, 2.45) |** 0.2%
( 2.45, 2.55) |** 0.2%
( 2.55, 2.65) |* 0.1%
( 2.65, 2.75) |* 0.1%
( 2.75, 2.85) |* 0.1%
( 2.85, 2.95) | 0.1%
 
press enter</pre>
 
=={{header|C sharp|C#}}==