Post

Real random number generator: a basic gaussian distribution

' gauss_rand_simple() ' is a function written in language C which return a real (double) value according to a gaussian distribution, that is mean 0 and variance 1 .         #include <stdlib.h>         #include <time.h> //necessary for time_t variable         #include <math.h>         #include <stdio.h>                           #define NUM 100;         #define NSUM 25          double gauss_rand_simple() {            double x = 0;            int i;            for(i = 0; i < NSUM; i++)                x += (double)rand() / RAND_MAX;                x -= NSUM / 2.0;                x /= sqrt(NSUM / 12.0);                return x;            }        }              int main(int argc, char* argv[]) {            time_t t;            srand((unsigned int) time(&t));            double val = 0;            for (int i=0; i < 10; ++i) {                       val = gauss_rand_simple();                 printf("%f\n",  val);  

Real Random number generator: Uniform distribution

'uniform_dist ' is a function written in language C which takes as input two real (double) numbers which represent a range, and return a real (double) value according to a Uniform distribution within the given range . The range doesn't include low and high (we will see this later).         #include <stdlib.h>         #include <time.h> //necessary for time_t variable         #include <stdio.h>               double uniform_dist(double low, double high) {              return ( (double)rand() / RAND_MAX) * (high - low) + low;        }       #define NUM 100;        int   main(int argc, char* argv[]) {               time_t t;               srand((unsigned int)time(&t));               double low = 1.5, high = 4.5;               for (int i=0; i < NUM; ++i) {                  printf("%f\n", uniform_dist(low, high) );               }         return 0;        } int rand(void) returns a random integer number in the range