A random number generator

J

Joseph

Hi all,

I am a newbie with a problem,I want to have a random number generator
which generate numbers satisfied normal distribution(gaussian).I found
that GNU GSL lib provids some functions which could do this ,but i still
can not control the mean value and upper/lower limit it generated.below
is the code :

===================================
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>

int main (void)
{
const gsl_rng_type * T;
gsl_rng * r;

// select random number generator
r = gsl_rng_alloc (gsl_rng_mt19937);

double sigma = 10;
for (int i = 0; i < 10; i++)
{
double v = gsl_ran_gaussian_tail(r,4.5, sigma);
}

gsl_rng_free (r);

return 0;
}
===============================

one problem is ,it does not cut off from 4.5 and generated very big
numbers,i want to control the output ,say,between 0-100 with mean 50,is
there any way could do that ?

Thanks a lot,guys!!
 
B

Brooks Moses

Joseph said:
I am a newbie with a problem,I want to have a random number generator
which generate numbers satisfied normal distribution(gaussian).I found
that GNU GSL lib provids some functions which could do this ,but i still
can not control the mean value and upper/lower limit it generated.below
is the code :

You are asking for an impossibility; numbers which satisfy a gaussian
distribution will not fit within a fixed upper and lower limit. If you
require that the numbers fit within a fixed limit, you no longer have a
gaussian distribution.

I would suggest that you do a little bit of reading on statistical
distributions, find a distribution that fits your requirements, and then
find (or write) some code that implements that distribution. I'm fairly
sure that what you want is a Beta distribution (properly shifted and
scaled); see the url below for details, and do a Google search on "beta
distribution" to figure out how the parameters relate to the mean and
variance.

Meanwhile, from your code:
[...]
double v = gsl_ran_gaussian_tail(r,4.5, sigma);
[...]

There seems to be a reference on this function here:
http://www.lsw.uni-heidelberg.de/manuals/gsl-ref-html/gsl-ref_18.html

It states that this function returns values from the gaussian tail, with
a cut-off such that they will all be greater than 4.5. This seems to
exactly match the problem that you are complaining of.

- Brooks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top