I have to write a C program to convert a uniform distribution of random numbers (say from 0 to 1) to a poisson distribution. Can anyone help?
Use GSL, the Gnu Scientific Library. There's a function called gsl_ran_poisson:
This function returns a random integer from the Poisson distribution with mean mu.
The probability distribution for Poisson variates is,
p(k) = {\mu^k \over k!} \exp(-\mu)
for k >= 0.
Otherwise, look at the code and copy the ideas.
I am assuming you want to write a C program that can sample a random number from the Poisson Distribution, given a random number in U(0,1).
Generally, this is done by taking the inverse CDF of the number from U(0,1). For discrete distributions like Poisson, one first transforms it to a continuous distribution by assuming that the CDF function is smooth between the integer points, and then we apply appropriate approximations (floor function).
The book Numerical Recipes in C++ (3rd Ed) has the complete explanation and C++ code as well. sec 7.3.12, page 372.
Related
If I do rand()/RAND_MAX, will it give me a random probability value?
If I do so, is it going to be that 50% (on average) of the values will be more than 0.5?
Never use rand() for any purpose, ever.
random() is likely suitable for your needs. (#include <stdlib.h>) It generates a uniform distribution in the range 0..231-1. random() / (double)((1L << 31) - 1) should get you close to a uniform distribution between 0.0 and 1.0.
You can use srandomdev() to seed it in order to get a different sequence every time.
Here is a histogram of one billion values returned by random() in 256 bins over the range 0..231-1:
If you look closely, you can see the expected tiny variations from uniform along the top of the histogram.
Yes the rand() / (double)RAND_MAX will give you a random value.
No, rand() have no mention of a uniform(or any other) distribution. There is no guarantee that "is it going to be that 50% (on average)".
Have a uniform distribution in C is a different question. You may be interested in Generating a uniform distribution of INTEGERS in C
SO topic.
I am writing a program in C that requires generating a normal distribution of positive integers with mean less than 1.
You can normalize data that is already normally distributed, for example take data for average length of human beings (180 centimeter) and scale every number by a factor so that the mean becomes less than 1 e.g. multiply every length by 1/180.
I used a Poisson random number generator function in C, which takes the mean as input. I used a combination of rand() followed by exponentiation to get the distribution, which is the normal way to do this, as in Calculation of Poisson distribution in C.
Since I generate 2^14 random numbers, by Central Limit theorem, their distribution will tend to a normal distribution, with the same mean and variance.
I want to generate uncorrelated random number to do a simulation... However, the numbers generated by the rand() function in the C language are correlated. Is there any possibility to use the rand() function and generate multiple random streams? I mean, if the rand() function generate for me a series of correlated numbers, can I cut this series into different streams. Then use these streams independently?
Thanks
You are indeed correct. They are normally autocorrelated as the normal generator implementation is linear congruential (although the C standard does not mandate this). As such an x-y plot of successive numbers will fail a chi square test for random 2D dispersion.
Depending on your application, you could look at Bays-Durham shuffle which, to my knowledge, passes the diehard test for randomness: it's aim is to defeat autocorrelation effects.
I direct you to www.nr.com for an implementation and the rand1, rand2 functions in particular. A more modern way is to use a mersenne twister scheme but a little tricker to implement (by the way C++11 has this generator as part of its standard library).
If your C implementation has rand_r, you can try that. It lets you specify a location to store the state.
Or just use your own pseudo-random number generator.
You may use arc4random or better ar4random_uniform to increase randomness of generated values (actually ar4random_uniform proves you uniformly distributed values).
Generating true random numbers on a computer is impossible, you can only generate "pseudo-random" numbers i.e. numbers that "looks like" random.
Usually one will use a ''seed'' (small sequence of bits) with enough entropy and then "expand" it thanks to a Pseudo-Random-Number-Generator.
C rand() function generates poor quality of randomness, try PRNG that have been proposed in other answers/comments. Some examples:
Mersenne Twister (widely used)
ANSI X9 (adopted by FIPS standard)
I am trying to generate the Gaussian Distribution using Method 2 and Method 3 described here:
http://c-faq.com/lib/gaussian.html
The problem is I am little confused as I have sigma and Mean and the 100 numbers with the range of 0 to 1 but in these methods it just returns the value for the interval of 0 to 1 and in these methods the sigma and mean value has not been used.
Can anyone help me how can I generate a Gaussian distribution using these methods?
the routines you linked to give random numbers (selected from a gaussian (normal) distribution) with mean 0 and standard deviation 1. that's the usual way routines like these work.
it's quite easy to change that to any other mean and sd - you just multiple by the sd and then add the mean.
so, for example, if x was generated by one of the routines above, then
y = 0.5 * x + 0.1
will have a standard deviation of 0.5 and a mean of 0.1.
so you don't need a separate routine for each combination of mean and sd. you just use the routines as given and then do the extra conversion.
i need a library with functions for generating random number, given average, standard deviation and using one of three distribution - exponential, normal or unified.
even one of the three would help.
i'm looking for something like this - http://www.codeproject.com/KB/recipes/zigurat.aspx, but in c.
thanks
May I recommend the GNU Scientific Library either for use or for inspiration? It has several Random Number Distributions and is designed to be used from C and C++.
uniform:
Generate a random number in the range [0,1] with uniform distribution:
double X=((double)rand()/(double)RAND_MAX);
Exponentional
generating an exponentional random variable with parameter lambda:
-ln(U)/lambda (where U~Uniform[0,1]).
normal:
the simplest way [though time consuming] is using the central limit theorem, [sum enough uniformly distributed numbers] but there are other methods in the wikipedia page such as the box muller transform that generates 2 independent random variables: X,Y~N(0,1)
X=sqrt(-2ln(U))*cos(2*pi*V)
Y=sqrt(-2ln(U))*sin(2*pi*V)
where U,V~UNIFORM[0,1]
transforming from X~N(0,1) to Z~N(m,s^2) is simple: Z = s*X + m
Though you CAN generate these random numbers, I stand by #Amigable Clark Kant suggestion to use an existing library.