Code:
/* Placing randomly generated numbers into various bins on the number line to test the how the probability varies by distance from the mean */
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include "add_array.h"
int main(int argc, char *argv[])
{
int N,i,M,j=0;
double Xo, mean, sigma, variance, *a, total, X, Y, *bin,tmp=0;
{
if(argc != 6)
{
printf("Error!\nUsage:N(int) Xo(double) sigma(double) Y(double) M (int)\n");
return 0;
}
else
sscanf( argv[1], "%d", &N );
}
sscanf( argv[2], "%lf", &Xo);
sscanf( argv[3], "%lf", &sigma);
sscanf( argv[4], "%lf", &Y);
sscanf( argv[5], "%d", &M);
const gsl_rng_type * T;
gsl_rng * r;
gsl_rng_env_setup();
T = gsl_rng_default;
r = gsl_rng_alloc (T);
gsl_rng_set (r, time(NULL));
a = (double *) malloc (N * sizeof *a);
bin = (double *) malloc ((M + 1) * sizeof *bin);
printf ("\nInput Parameters: \n\n%d %lf %lf %lf %d\n", N, Xo, sigma, Y, M);
printf ("\nLength of Bins: \n");
for (i = 0; i <= M; i++)
{
double k = -Y + 2 * i * Y / M;
bin[i] = k;
}
for (i = 0; i < M; i++)
{
printf ("\n%.2f to %.2f", bin[i], bin[i+1]);
}
printf ("\n");
for (i = 0; i < N; i++)
{
double u = gsl_ran_gaussian(r,sigma);
a[i] = u + Xo;
}
for(i = 0; i < N; i++)
{
for(j = 0; j < N-1-i; j++)
{
if (a[j] > a[j + 1])
{
tmp = a[j + 1];
a[j + 1] = a[j];
a[j] = tmp;
}
}
}
printf("\nAscending Order:\n");
for(i = 0; i < N; i++)
{
printf("a[%d] = %f\n", i, a[i]);
}
printf("\n");
for(j = 0; j < M-1; j++)
{
for(i = 0; i < N-1; i++)
{
if (a[i] >= bin[j] && a[i] < bin[j+1])
printf("1");
else
printf("O");
}
printf("\n");
}
total= add_array(a, N);
X = add_squares(a, N);
//printf("\nX = %lf\n", X);
mean = total/N;
variance = X/N;
//printf ("\nTotal:%lf\nMean:%lf\nVariance:%lf\n", total, mean, sqrt(variance));
free(a);
gsl_rng_free (r);
return 0;
}
I wanted to play around with the code but I was having trouble with a couple things:
1) code should work for arbitrary mean (edit: got it )
2) code should handle outliers correctly (sometimes the random number generator spits out ocassional outliers that the program does not cover, I need to find a way to check the number before I run it through thr for-loop)
3) instead of generating a list of randomly generated numbers, I want to generate one at a time and test them for which bin they would fall under individually...
I'm trying to edit all of the above things at once from my original code, because I can't successfully figure out how to do each individual one