Skip to content

Instantly share code, notes, and snippets.

@meta-ks
Created April 15, 2019 10:36
Show Gist options
  • Save meta-ks/3fd6baf3b3a862c73d0feb2ffdfd9e0e to your computer and use it in GitHub Desktop.
Save meta-ks/3fd6baf3b3a862c73d0feb2ffdfd9e0e to your computer and use it in GitHub Desktop.
#include <iostream>
#include <chrono>
#include <random>
#define DOUBLE_PRECISION 10000
#define LAMBDA 5
using namespace std;
struct packet {
int data;
unsigned int src_port;
unsigned int dst_port;
struct packet *next;
};
struct packet* front = NULL;
struct packet* rear = NULL;
struct packet* temp;
void Insert(struct packet * rear, int val, unsigned int src_port, unsigned int dst_port) {
cout<<"going to Insert " << val << " in queue : "<<endl;
if (rear == NULL) {
rear = (struct packet *)malloc(sizeof(struct packet));
rear->next = NULL;
rear->data = val;
rear->src_port = src_port;
rear->dst_port = dst_port;
front = rear;
} else {
temp=(struct packet *)malloc(sizeof(struct packet));
rear->next = temp;
temp->data = val;
temp->src_port = src_port;
temp->dst_port = dst_port;
temp->next = NULL;
rear = temp;
}
}
void Delete() {
temp = front;
if (front == NULL) {
cout<<"Underflow"<<endl;
return;
}
else
if (temp->next != NULL) {
temp = temp->next;
cout<<"Element deleted from queue is : "<< front->data <<endl;
free(front);
front = temp;
} else {
cout<<"Element deleted from queue is : "<<front->data<<endl;
free(front);
front = NULL;
rear = NULL;
}
}
void Display() {
temp = front;
if ((front == NULL) && (rear == NULL)) {
cout<<"Queue is empty"<<endl;
return;
}
cout<<"Queue elements are: ";
while (temp != NULL) {
cout<< temp->data <<", "<< temp->src_port <<", "<< temp->dst_port <<" ";
temp = temp->next;
}
cout<<endl;
}
unsigned int factorial(unsigned int n)
{
if(n == 0) return 1;
int res = 1, i;
for (i = 2; i <= n; i++)
res *= i;
return res;
}
int poisson_rand(int lambda)
{
double temp;
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
srand(seed);
temp = rand() % DOUBLE_PRECISION;
double uni_rand = temp/DOUBLE_PRECISION;
cout << "Using uniform rand as: " << uni_rand << endl;
double sum = 0;
for(int k=0; sum <= uni_rand; k++)
{
sum += exp(-lambda)*pow(lambda,k)/factorial(k);
if (sum > uni_rand)
{
if(k>0)
return (k-1);
else
return(k);
}
}
}
int main()
{
int iter_count = 100;
int buff_sz = 10, arr_rate = 5, dep_rate = 50;
for(int i=1; i<iter_count ; i++)
{
int p_rand = poisson_rand(10);
cout << "[" << i << "] " << "Poisson distributed no (lambda = " << LAMBDA << "): " << p_rand << endl;
cout << endl;
Insert(rear, p_rand, rand()%1000, rand()%1000);
}
Display();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment