Five real random numbers x0 from the Gaussian distribution (0,1):
Five real random numbers x1 from the Gaussian distribution (mu,sigma) :
There is the following relation : x1 = x0*sigma + mu
Therefore, from a real random number from t distribution random number generator with nu, I can get the random magnetic field strength B_t as following:
,where t_random is generated by gsl_ran_tdist function as
#include <stdio.h> #include <gsl/gsl_rng.h> #include <gsl/gsl_randist.h> double gsl_ran_tdist (const gsl_rng * r, double nu) ,where nu is the degrees of freedom.
In the random_t_distribution function, I used a sort of random number generator in order to get a random seed. The original random number generator, which is based on /dev/random, is shown in Advanced Linux Programming Chapter 6. I modified it as following:
#include <stdio.h> #include <assert.h> #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> #include <unistd.h> unsigned long random_seed_number() { /* Store a file descriptor opened to /dev/random in a static variable. That way, we don't need to open the file every time this function is called. */ static int dev_random_fd = -1; char* next_random_byte; int bytes_to_read; unsigned long random_value;
if (dev_random_fd == -1) { dev_random_fd = open ("/dev/random", O_RDONLY); assert (dev_random_fd != -1); }
/* Read enough random bytes to fill an integer variable. */ next_random_byte = (char*) &random_value; bytes_to_read = sizeof (random_value);
/* Loop until we've read enough bytes. Because /dev/random is filled from user-generated actions, the read may block and may only return a single random byte at a time. */ do { int bytes_read; bytes_read = read (dev_random_fd, next_random_byte, bytes_to_read); bytes_to_read -= bytes_read; next_random_byte += bytes_read; } while (bytes_to_read > 0); return random_value; }