ConicBundle
gb_rand.hxx
Go to the documentation of this file.
1 
2 
3 #ifndef CH_TOOLS__GB_RAND_HXX
4 #define CH_TOOLS__GB_RAND_HXX
5 
6 #include <iostream>
7 
18 namespace CH_Tools {
19 
23 
28 class GB_rand
29 {
30 private:
31  long A[56];
32  int ind;
33 
35  long mod_diff(long x,long y){return ((x-y)&0x7fffffff);}
36 
38  long flip_cycle ()
39  {
40  long *ii, *jj;
41  for (ii = &A[1], jj = &A[32]; jj <= &A[55]; ii++, jj++)
42  *ii = ((*ii-*jj) & 0x7fffffff);
43  for (jj = &A[1]; ii <= &A[55]; ii++, jj++)
44  *ii = ((*ii-*jj) & 0x7fffffff);
45  ind = 54;
46  return A[55];
47  }
48 
49 public:
51  void init(long seed=1)
52  {
53  long i;
54  long prev = seed, next = 1;
55  seed = prev = (prev & 0x7fffffff);
56  A[55] = prev;
57  for (i = 21; i; i = (i + 21) % 55) {
58  A[i] = next;
59 
60  next = ((prev-next) & 0x7fffffff);
61  if (seed & 1)
62  seed = 0x40000000 + (seed >> 1);
63  else
64  seed >>= 1;
65  next = ((next- seed) & 0x7fffffff);
66 
67  prev = A[i];
68  }
69 
70  (void) flip_cycle ();
71  (void) flip_cycle ();
72  (void) flip_cycle ();
73  (void) flip_cycle ();
74  (void) flip_cycle ();
75 
76  }
77 
79  GB_rand(long seed=1)
80  {ind=0;A[0]=-1;init(seed);}
81 
83  ~GB_rand(){}
84 
86  long unif_long(long m)
87  {
88  const unsigned long two_to_the_31 = (unsigned long)0x80000000;
89  unsigned long t = two_to_the_31 - (two_to_the_31 % (unsigned long)(m));
90  long r;
91  do {
92  r = (A[ind]>=0?(A[ind--]):flip_cycle());
93  } while (t <= (unsigned long) r);
94  return r % m;
95  }
96 
98  double next()
99  {
100  long r=unif_long(0x40000000);
101  return (double(r)+.5)/double(0x40000000);
102  }
103 
105  std::ostream& save(std::ostream& out) const
106  {
107  for(int i=0;i<56;i++) out<<A[i]<<"\n";
108  out<<ind<<"\n"; return out;
109  }
110 
112  std::istream& restore(std::istream& in)
113  {
114  for(int i=0;i<56;i++)
115  in>>A[i];
116  in>>ind; return in;
117  }
118 
119 };
120 
122 
123 }
124 
125 #endif
126 
Some convenient inline tools like a clock for timing, a random number generator, a heapsort template ...
Definition: clock.hxx:29
long mod_diff(long x, long y)
compute (x-y) & 0x7fffffff
Definition: gb_rand.hxx:35
int ind
index of the next number to return
Definition: gb_rand.hxx:32
long unif_long(long m)
returns a random integer number "uniformly distributed" in {0,..,m-1}
Definition: gb_rand.hxx:86
device independent random number generator based on long int with seed
Definition: gb_rand.hxx:28
long flip_cycle()
compute next set of numbers
Definition: gb_rand.hxx:38
std::istream & restore(std::istream &in)
restore settings as stored in save
Definition: gb_rand.hxx:112
std::ostream & save(std::ostream &out) const
save current configuration to out so as to continue identically after restore
Definition: gb_rand.hxx:105
void init(long seed=1)
restart generator with seed
Definition: gb_rand.hxx:51
double next()
returns a random double number "uniformly distributed" in (0,1)
Definition: gb_rand.hxx:98
GB_rand(long seed=1)
calls init(seed)
Definition: gb_rand.hxx:79
long A[56]
storage for the next few precomputed random numbers
Definition: gb_rand.hxx:31