Use C language to generate Poisson distribution random number instance source code

Source: Internet
Author: User
Tags mongodb postgresql redis

Helping a friend do a job in graduate management is a simulation of the inventory system, where the retailer's demand is a random number with Poisson distribution. We need to use C language to generate random numbers with Poisson distribution. By looking for materials and programming practices, a simple program is written as follows for your reference.

The code is as follows:
Algorithm poisson random number (Knuth ):
Init:
Let L & larr; exp (& minus; & lambda;), k & larr; 0 and p & larr; 1.
Do:
K & larr; k + 1.
Generate uniform. random number u in [0, 1] and let p & larr; p × u.
While p> = L.
Return (k & minus; 1 ).



The code for the Poisson distribution random number in C language is as follows:

The code is as follows:
# Include <stdio. h>
# Include <math. h>
# Include <time. h>
 
Double U_Random ();
Int possion ();
 
 
Void main ()
{
Double u = U_Random ();
Int p = possion ();
Printf ("% fn", u );
Printf ("% dn", p );
 
}
 
Int possion ()/* generates a random number with a Poisson distribution. Lamda is the average number */
{
Int Lambda = 20, k = 0;
Long double p = 1.0;
Long double l = exp (-Lambda);/* it is defined as long double for precision, and exp (-Lambda) is a decimal near 0 */
Printf ("%. 15Lfn", l );
While (p> = l)
        {
Double u = U_Random ();
P * = u;
K ++;
        }
Return K-1;
}
 
Double U_Random ()/* generates a 0 ~ Random number between 1 */
{
Double f;
Srand (unsigned) time (NULL ));
F = (float) (rand () % 100 );
/* Printf ("% fn", f );*/
Return f/100;
}



All the code for this simple inventory simulation: https://github.com/smilejay/c-cpp/tree/master/inventory-simulation

Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

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.