The implementation of C-language Poisson distribution and exponential distribution random number generator

Source: Internet
Author: User

Recent lab projects need to implement a simulated file access sequence that requires the number of data requests per unit time to be in accordance with the Poisson distribution, while the time interval of two requests meets the exponential distribution. There is no way to re-pick up the probability that has been lost a long knowledge. Then there is the implementation of the random number generator which conforms to the Poisson distribution and exponential distribution in C language.

Poisson distribution

In the actual case, when an event, such as the number of incoming passengers, the call request received by the telephone switch at a fixed instantaneous rate λ is independent and randomly appears, it can be considered that the number of times that the event occurred within the unit time is in accordance with the Poisson distribution.

First, there must be a two-item distribution:

If the probability of success is p, then try to do it independently n times, and the distribution of the number of successes matches two distributions. In terms of n times, the number of successes is likely to be 0, 1 times ... n times. The probability of success I times is:

(Number of combinations of I items selected in N) * p ^ i * (1-p) ^ (n-i)

The above formula is easy to deduce, with a little probability to learn the most basic knowledge is enough. Because the probability of success for each particular event is p, the probability of unsuccessful is 1-p. I successful events can be arbitrarily distributed in a total of n attempts. To multiply them is the probability of just succeeding I.

When we generalize the two distributions, we can get the bouasone distribution.

It can be considered that, at a given time, something happens randomly at any point (provided that each occurrence is independent and time-dependent). When we divide this period into very small time slices, it can be assumed that the event may or may not occur within each time slice. It is almost impossible to think about more than one occurrence (since the time slice is small enough to be divided).

When the time slice is smaller, the probability that the event occurs in this time slice will be proportional to the decrease. That is: The number of time slices divided into a particular time period n and the probability of the occurrence of each time on-chip event p product n*p to a constant. This constant indicates how often the event occurs over a specified period of time.

Look back and see what the probability of the occurrence of an event happens to me in this time period? Substituting the formula deduced above to get:

n * (n-1) ... (n-i+1)/I! * P^i * (1-p) ^ (n-i) = np(np-p) ... (np-ip+p)/i! * ((1-p) ^ ( -1/p)) ^ (-nP)/(1-P) ^i

P tends to 0 when n tends to infinity. While at this time (1-p) ^ ( -1/p) the trend E.

The above formula can be drawn Jianwei

The average rate at which the λ event occurred.

With this knowledge, it is not difficult to write a random number generator program that fits the Poisson distribution:

int possion (int Lambda) {int  k = 0;long Double p = 1.0;long Double L = exp (-LAMBDA);        Srand ((unsigned) time (NULL));       //printf ("%1.5lf\n", L), while (p>=l) {Double u = u_random ();p *= u;k++;} return k-1;} Double U_random () {Double f;f = (float) (rand ()%100); return f/100;}

Exponential distribution

Exponential distribution is a continuous probability distribution, which can often be used to describe the time interval in which successive independent random events occur. The probability density function is:

which λ is the average rate at which events occur.

For the simpler probability distribution, the random number generator can be constructed by the inverse function of its corresponding cumulative distribution function. The specific proof can refer to this article: click the Open link. For exponential distributions, the cumulative distribution function is:

Then the inverse function is:


So its random number generator program is:

Double randomexponential (double lambda) {Double PV = 0.0;PV = (double) (rand ()%100)/100;while (PV = = 0) {PV = (double) (rand () % 100)/100;} PV = ( -1  /lambda) *log (1-PV); return PV;}




The implementation of C-language Poisson distribution and exponential distribution random number generator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.