3

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*/
    }

}

2 Answers 2

1

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*/
    }

}
7
  • I mean, if you see the reference link and link for the same time, based on the Lamda variable, the bigger Lamda, the more I get, and the less Lamda, the less I get print.. Yes, I think I shouldn't bound loop to ten, how to fix this?
    – xambo
    Nov 30, 2011 at 20:23
  • uhm, sorry for my stupid. but I read your code, it is basically doing the same thing.. (except infinite loop)?
    – xambo
    Nov 30, 2011 at 20:50
  • @xambo: you read just the code, not the comments? Anyways, posting a completed version right now
    – sehe
    Nov 30, 2011 at 20:56
  • A fully C++11 version of it is now complete, see updated answer PS. Here it is live on IDEONE: ideone.com/viZi3
    – sehe
    Nov 30, 2011 at 21:09
  • I cannot help myself but give you +1! Nov 30, 2011 at 21:13
1

Given your code, you'll always have message printed 10 times. Seems that you need to check if your total time elapsed at the start of loop, and if so, break loop. To give you an idea:

time_t before, timer;
...
time(&before);
for (...) {
    time(&timer);
    if (time - before > timeout) {
         break;
    }
    before = timer;
    ...
}
2
  • Sorry, I don't get you. what do you mean with "Seems that you need to check if your total time elapsed at the start of loop, and if so, break loop." ?
    – xambo
    Nov 30, 2011 at 20:20
  • @xambo I've expanded answer a bit. Hope, it will help. Nov 30, 2011 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.