RandomLib  1.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
RandomExact.cpp
Go to the documentation of this file.
1 /**
2  * \file RandomExact.cpp
3  * \brief Using %RandomLib to generate exact random results
4  *
5  * Compile/link with, e.g.,\n
6  * g++ -I../include -O2 -funroll-loops
7  * -o RandomExact RandomExact.cpp ../src/Random.cpp\n
8  * ./RandomExact
9  *
10  * See \ref otherdist, for more information.
11  *
12  * Copyright (c) Charles Karney (2006-2012) <charles@karney.com> and licensed
13  * under the MIT/X11 License. For more information, see
14  * http://randomlib.sourceforge.net/
15  **********************************************************************/
16 
17 #include <iostream>
18 #include <utility>
19 #include <RandomLib/Random.hpp>
28 
29 int main(int, char**) {
30  // Create r with a random seed
31  RandomLib::Random r; r.Reseed();
32  std::cout << "Using " << r.Name() << "\n"
33  << "with seed " << r.SeedString() << "\n\n";
34  {
35  std::cout
36  << "Sampling exactly from the normal distribution. First number is\n"
37  << "in binary with ... indicating an infinite sequence of random\n"
38  << "bits. Second number gives the corresponding interval. Third\n"
39  << "number is the result of filling in the missing bits and rounding\n"
40  << "exactly to the nearest representable double.\n";
41  const int bits = 1;
43  long long num = 20000000ll;
44  long long bitcount = 0;
45  int numprint = 16;
46  for (long long i = 0; i < num; ++i) {
47  long long k = r.Count();
48  RandomLib::RandomNumber<bits> x = ndist(r); // Sample
49  bitcount += r.Count() - k;
50  if (i < numprint) {
51  std::pair<double, double> z = x.Range();
52  std::cout << x << " = " // Print in binary with ellipsis
53  << "(" << z.first << "," << z.second << ")"; // Print range
54  double v = x.Value<double>(r); // Round exactly to nearest double
55  std::cout << " = " << v << "\n";
56  } else if (i == numprint)
57  std::cout << std::flush;
58  }
59  std::cout
60  << "Number of bits needed to obtain the binary representation averaged\n"
61  << "over " << num << " samples = " << bitcount/double(num) << "\n\n";
62  }
63  {
64  std::cout
65  << "Sampling exactly from exp(-x). First number is in binary with\n"
66  << "... indicating an infinite sequence of random bits. Second\n"
67  << "number gives the corresponding interval. Third number is the\n"
68  << "result of filling in the missing bits and rounding exactly to\n"
69  << "the nearest representable double.\n";
70  const int bits = 1;
72  long long num = 50000000ll;
73  long long bitcount = 0;
74  int numprint = 16;
75  for (long long i = 0; i < num; ++i) {
76  long long k = r.Count();
77  RandomLib::RandomNumber<bits> x = edist(r); // Sample
78  bitcount += r.Count() - k;
79  if (i < numprint) {
80  std::pair<double, double> z = x.Range();
81  std::cout << x << " = " // Print in binary with ellipsis
82  << "(" << z.first << "," << z.second << ")"; // Print range
83  double v = x.Value<double>(r); // Round exactly to nearest double
84  std::cout << " = " << v << "\n";
85  } else if (i == numprint)
86  std::cout << std::flush;
87  }
88  std::cout
89  << "Number of bits needed to obtain the binary representation averaged\n"
90  << "over " << num << " samples = " << bitcount/double(num) << "\n\n";
91  }
92  {
93  std::cout
94  << "Sampling exactly from the discrete normal distribution with\n"
95  << "sigma = 7 and mu = 1/2.\n";
96  RandomLib::DiscreteNormal<int> gdist(7,1,1,2);
97  long long num = 50000000ll;
98  long long count = r.Count();
99  int numprint = 16;
100  for (long long i = 0; i < num; ++i) {
101  int k = gdist(r); // Sample
102  if (i < numprint)
103  std::cout << k << " ";
104  else if (i == numprint)
105  std::cout << std::endl;
106  }
107  count = r.Count() - count;
108  std::cout
109  << "Number of random variates needed averaged\n"
110  << "over " << num << " samples = " << count/double(num) << "\n\n";
111  }
112  {
113  std::cout
114  << "Sampling exactly from the discrete normal distribution with\n"
115  << "sigma = 1024 and mu = 1/7. First result printed is a uniform\n"
116  << "range (with covers a power of two). The second number is the\n"
117  << "result of sampling additional bits within that range to obtain\n"
118  << "a definite result.\n";
119  RandomLib::DiscreteNormalAlt<int,1> gdist(1024,1,1,7);
120  long long num = 20000000ll;
121  long long count = r.Count();
122  long long entropy = 0;
123  int numprint = 16;
124  for (long long i = 0; i < num; ++i) {
126  entropy += u.Entropy();
127  if (i < numprint)
128  std::cout << u << " = ";
129  int k = u(r);
130  if (i < numprint)
131  std::cout << k << "\n";
132  else if (i == numprint)
133  std::cout << std::flush;
134  }
135  count = r.Count() - count;
136  std::cout
137  << "Number of random bits needed for full result (for range) averaged\n"
138  << "over " << num << " samples = " << count/double(num) << " ("
139  << (count - entropy)/double(num) << ")\n\n";
140  }
141  {
142  std::cout
143  << "Random bits with 1 occurring with probability 1/pi exactly:\n";
144  long long num = 100000000ll;
145  int numprint = 72;
147  long long nbits = 0;
148  long long k = r.Count();
149  for (long long i = 0; i < num; ++i) {
150  bool b = pp(r);
151  nbits += int(b);
152  if (i < numprint) std::cout << int(b);
153  else if (i == numprint) std::cout << "..." << std::flush;
154  }
155  std::cout << "\n";
156  std::cout << "Frequency of 1 averaged over " << num << " samples = 1/"
157  << double(num)/nbits << "\n"
158  << "bits/sample = " << (r.Count() - k)/double(num) << "\n\n";
159  }
160  {
161  std::cout
162  << "Random bits with 1 occurring with probability 1/e exactly:\n";
163  long long num = 200000000ll;
164  int numprint = 72;
166  long long nbits = 0;
167  long long k = r.Count();
168  for (long long i = 0; i < num; ++i) {
169  bool b = ep(r);
170  nbits += int(b);
171  if (i < numprint) std::cout << int(b);
172  else if (i == numprint) std::cout << "..." << std::flush;
173  }
174  std::cout << "\n";
175  std::cout << "Frequency of 1 averaged over " << num << " samples = 1/"
176  << double(num)/nbits << "\n"
177  << "bits/sample = " << (r.Count() - k)/double(num) << "\n";
178  }
179  return 0;
180 }
Header for UniformInteger.
Header for RandomNumber.
Return true with probability 1/e = exp(−1).
Generate random integers, reals, and booleans.
Infinite precision random numbers.
Header for Random, RandomGenerator.
int main(int, char **)
Definition: RandomExact.cpp:29
std::pair< double, double > Range() const
The partial uniform integer distribution.
The discrete normal distribution (alternate version).
Header for DiscreteNormal.
Header for ExactExponential.
RealType Value(Random &r)
The discrete normal distribution.
Header for InverseEProb.
Sample exactly from an exponential distribution.
Header for InversePiProb.
Sample exactly from a normal distribution.
Header for DiscreteNormalAlt.
Header for ExactNormal.
Return true with probability 1/π.