386320d266
This is extracted from another git repo. This is the first release, and the prior commit history is not terribly interesting, so I'm not going to bother using filter-branch to try to cleanly isolate the history for this tool. Cheers.
35 lines
799 B
C++
35 lines
799 B
C++
#include <math.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "distributions.h"
|
|
#include "log.h"
|
|
|
|
const char* distributions[] =
|
|
{ "uniform", "exponential", "zipfian", "latest", NULL };
|
|
|
|
distribution_t get_distribution(const char *name) {
|
|
for (int i = 0; distributions[i] != NULL; i++)
|
|
if (!strcmp(distributions[i], name))
|
|
return (distribution_t) i;
|
|
return (distribution_t) -1;
|
|
}
|
|
|
|
double generate_normal(double mean, double sd) {
|
|
double U = drand48();
|
|
double V = drand48();
|
|
double N = sqrt(-2 * log(U)) * cos(2 * M_PI * V);
|
|
return mean + sd * N;
|
|
}
|
|
|
|
double generate_poisson(double lambda) {
|
|
if (lambda <= 0.0) return 0;
|
|
double U = drand48();
|
|
return -log(U)/lambda;
|
|
}
|
|
|
|
double generate_uniform(double lambda) {
|
|
if (lambda <= 0.0) return 0;
|
|
return 1.0 / lambda;
|
|
}
|