Scheduler
randomGenerator.cpp
Go to the documentation of this file.
1 
10 #include "randomGenerator.h"
11 
12 #include <chrono>
13 #include <cmath>
14 #include <ctime>
15 #include <iostream>
16 
17 using namespace Utils;
18 
20 {
21  gen.seed(time(NULL));
22 }
23 
25 {
26  gen.seed(seed);
27 }
28 
29 double RandomGenerator::drawUniform(double min, double max)
30 {
31  double ret = unif(gen);
32  ret *= (max-min);
33  ret += min;
34  return ret;
35 }
36 
37 double RandomGenerator::drawExp(double lambda)
38 {
39  if (lambda <= 0)
40  {
41  return 0;
42  }
43  double uni = unif(gen);
44  return -log(uni)/lambda;
45 }
46 
47 
49 {
50  std::cout << "\033[1;32m" << "Using seed "<< seed << "\033[0m" << "\n"; //FIXME use the log class for color
51  gen.seed(seed);
52 }
53 
54 
55 int RandomGenerator::drawUniformInt(int min, int max)
56 {
57  double d;
58  do
59  {
60  d = drawUniform((double) min, (double) max + 1.0);
61  } while(d >= (double) max + 1.0);
62  int ret = (int) floor(d);
63  return ret;
64 }
65 
66 std::vector<double> RandomGenerator::drawDistribution(int nbOfBins)
67 {
68  std::vector<double> vector(nbOfBins);
69  double total = 0.0;
70  for (int i = 0; i < nbOfBins; i++)
71  {
72  vector[i] = drawUniform(0.0, 1.0);
73  total += vector[i];
74  }
75  for (int i = 0; i < nbOfBins; i++)
76  {
77  vector[i] /= total;
78  }
79  return vector;
80 }
81 
82 double RandomGenerator::drawBeta(double /*a*/, double /*b*/)
83 {
84  /*TODO*/
85  return 0.0;
86 }
void seed(time_t seed)
list time
Definition: aging.py:11
double drawBeta(double alpha, double beta)
double drawExp(double lambda)
Definition: context.h:16
std::vector< double > drawDistribution(int nbOfBins)
double drawUniform(double min, double max)
int drawUniformInt(int min, int max)