Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef MTGL_RANDOM_H
00022 #define MTGL_RANDOM_H
00023
00024 #include <cstdlib>
00025
00026 #include <mtgl/util.hpp>
00027 #include <mtgl/rand_fill.hpp>
00028
00029 namespace mtgl {
00030
00031 class random {
00032 public:
00033 random(long int sd = 0, int sz = 5000) : seed(sd), size(sz)
00034 {
00035
00036 _store = (random_value*) malloc(size * sizeof(random_value));
00037 rand_fill::generate(size, _store);
00038 current = 0;
00039 refills = 0;
00040 numTaken = 0;
00041 }
00042
00043
00044
00045 random_value nthValue(int rank)
00046 {
00047 random_value result;
00048 bool my_values_ready = false;
00049 int index = rank % size;
00050 int turn = rank / size;
00051
00052 while (!my_values_ready)
00053 {
00054 int nrefills = mt_readff(refills);
00055 if (nrefills == turn) my_values_ready = true;
00056 }
00057
00058 result = _store[index];
00059 int nt = mt_incr(numTaken, 1);
00060
00061 if (nt == size - 1)
00062 {
00063 rand_fill::generate(size, _store);
00064 mt_incr(numTaken, -numTaken);
00065 mt_incr(refills, 1);
00066 }
00067
00068 return result;
00069 }
00070
00071 ~random() { free(_store); }
00072
00073 private:
00074 long int seed;
00075 int size;
00076 int current;
00077 int numTaken;
00078 random_value* _store;
00079 int refills;
00080 };
00081
00082 }
00083
00084 #endif