Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Poisson arrival distribution function and keep track it

I am trying to create a random "hello world" function based on the poisson arrival. In the code below, I define that the average mean (Lamda) is 5. And I want the time to elapse from 1 - 5 second, and keep track of it.

Based on an opensource project, seagull in this image here and here, I can see that for the same time, but different mean, the more it random occurences of the traffic (in my case, the "hello world"). But for my case, it just getting random sleep time, but the number of Hello World is the same.

How can I achieve the idea based on the images like what I use above. Is this the correct way of doing Poisson distribution for random generator? I saw the algorithm for Poisson based on Knuth

Thank you for the help.. Sorry for my bad english.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#include <string.h>
#include <time.h>

int poisson(double lambda){
  int k=0;
  double L=exp(-lambda), p=1;
  do {
    ++k;
    p *= rand()/(double)INT_MAX;
  } while (p > L);
  return --k;
}

int main()
{
int i=0; 
int val=0;
time_t timer;
char buffer[25];
struct tm* val_time;



    /*For time= 0 until time=10*/
    for  (i=0; i<10; i++)
    {
    printf("Hello World\n");

    /*To print the time*/
    time(&timer);
    val_time = localtime(&timer);
    strftime(buffer, 25, "%Y:%m:%d%H:%M:%S", val_time);
    puts(buffer);

    sleep(poisson(2)); /*interarrival process*/
    }

}
like image 561
xambo Avatar asked Jun 04 '26 14:06

xambo


1 Answers

I think the INT_MAX is in error, make that:

    p *= rand()/(double)RAND_MAX;

Also, as long as the loop is bounded at 10, you'll not get more hellos. What do you expect?

Here is my full C++11 (not C!) version of the program:

See it live on https://ideone.com/viZi3 (Note it soft-fails with Time limit exceeded there, because of obvious time-constraints on IdeOne)

#include <iostream>
#include <random>
#include <chrono>
#include <iomanip>

static std::mt19937 rng;
static std::poisson_distribution<int> poisson(2.0);

typedef std::chrono::high_resolution_clock Clock;
typedef std::chrono::time_point<Clock> Time;

int main()
{
    const Time finish_pole = Clock::now() + std::chrono::seconds(10);

    for (Time now = Clock::now(); now <= finish_pole; now = Clock::now())
    {
        std::cout << "Hello World\n";

        std::time_t now_c = Clock::to_time_t(now);
#if CXX11_SUPPORT_COMPLETE
        std::cout << std::put_time(std::localtime(&now_c), "%F %T") << std::endl;
#else
        char buffer[25];
        strftime(buffer, 25, "%Y:%m:%d%H:%M:%S", localtime(&now_c));
        std::cout << buffer << std::endl;
#endif

        sleep(poisson(rng)); /*interarrival process*/
    }

}
like image 96
sehe Avatar answered Jun 07 '26 09:06

sehe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!