This is the mail archive of the
gsl-discuss@sourceware.cygnus.com
mailing list for the GSL project.
Re: A small help required
I have added an example program to the Random Number Distributions
chapter in the manual which should be what you need. The info page is
attached below.
regards
Brian Gough
Chetan Kumar writes:
> Hi All,
> I was looking for simple sample program that can helpme out, in
> starting with gsl. Precisly I would be happy if someone could help me
> with a sample program that generate a poisson random number.
>
> Thanks
> -Chetan S
>
> To err is human, and I want to be human..
File: gsl-ref.info, Node: Random Number Distribution Examples, Next: Random Number Distribution References and Further Reading, Prev: Shuffling and Sampling, Up: Random Number Distributions
Random Number Distribution Examples
===================================
The following program demonstrates the use of a random number
generator to produce variates from a distribution. It prints 10 samples
from the Poisson distribution with a mean of 3.
#include <stdio.h>
#include <gsl_rng.h>
#include <gsl_randist.h>
int
main ()
{
gsl_rng * r ;
int i, n = 10;
double mu = 3.0;
/* create a generator chosen by the environment variable GSL_RNG_TYPE */
gsl_rng_env_setup();
r = gsl_rng_alloc (gsl_rng_default);
/* print n random variates chosen from the poisson distribution with
mean parameter mu */
for (i = 0; i < n; i++)
{
unsigned int k = gsl_ran_poisson (r, mu);
printf(" %u", k);
}
printf("\n");
}
If the library and header files are installed under `/usr/local' (the
default location) then the program can be compiled with these options,
gcc -I/usr/local/include/gsl -L/usr/local/lib/gsl demo.c -lgslrandist -lgslrng -lgslspecfunc -lgslerr -lm
Here is the output of the program,
$ ./a.out
4 2 3 3 1 3 4 1 3 5
The variates depend on the seed used by the generator. The seed for the
default generator type `gsl_rng_default' can be changed with the
`GSL_RNG_SEED' environment variable to produce a different stream of
variates,
$ GSL_RNG_SEED=123 ./a.out
GSL_RNG_SEED=123
1 1 2 1 2 6 2 1 8 7