RandomLib  1.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
RandomExample.cpp
Go to the documentation of this file.
1 /**
2  * \file RandomExample.cpp
3  * \brief Simple examples of use of %RandomLib
4  *
5  * Compile/link with, e.g.,\n
6  * g++ -I../include -O2 -funroll-loops
7  * -o RandomExample RandomExample.cpp ../src/Random.cpp\n
8  * ./RandomExample
9  *
10  * This provides a simple illustration of some of the capabilities of
11  * %RandomLib.
12  *
13  * Copyright (c) Charles Karney (2006-2011) <charles@karney.com> and licensed
14  * under the MIT/X11 License. For more information, see
15  * http://randomlib.sourceforge.net/
16  **********************************************************************/
17 
18 #include <iostream>
19 #include <vector>
20 #include <string>
21 #include <algorithm>
22 #include <RandomLib/Random.hpp>
25 
26 int main(int, char**) {
27  RandomLib::Random r; // Create r
28  r.Reseed(); // and give it a unique seed
29  std::cout << "Using " << r.Name() << "\n"
30  << "with seed " << r.SeedString() << "\n";
31  {
32  std::cout << "Estimate pi = ";
33  size_t in = 0, num = 10000;
34  for (size_t i = 0; i < num; ++i) {
35  // x, y are in the interval (-1/2,1/2)
36  double x = r.FixedS(), y = r.FixedS();
37  if (x * x + y * y < 0.25) ++in; // Inside the circle
38  }
39  std::cout << (4.0 * in) / num << "\n";
40  }
41  {
42  std::cout << "Tossing a coin 20 times: ";
43  for (size_t i = 0; i < 20; ++i) std::cout << (r.Boolean() ? "H" : "T");
44  std::cout << "\n";
45  }
46  std::cout << "Generate 20 random bits: " << r.Bits<20>() << "\n";
47  {
48  std::cout << "Throwing a pair of dice 15 times:";
49  for (size_t i = 0; i < 15; ++i)
50  std::cout << " " << r.IntegerC(1,6) + r.IntegerC(1,6);
51  std::cout << "\n";
52  }
53  {
54  // Weights for throwing a pair of dice
55  unsigned w[] = { 0, 0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 };
56  // Initialize selection
57  RandomLib::RandomSelect<unsigned> sel(w, w + sizeof(w)/sizeof(unsigned));
58  std::cout << "A different way of throwing dice:";
59  for (size_t i = 0; i < 15; ++i) std::cout << " " << sel(r);
60  std::cout << "\n";
61  }
62  {
63  std::cout << "Draw balls from urn containing 5 red and 5 white balls: ";
64  int t = 10, w = 5;
65  while (t) std::cout << (r.Prob(w, t--) ? w--, "W" : "R");
66  std::cout << "\n";
67  }
68  {
69  std::cout << "Shuffling the letters a..z: ";
70  std::string digits = "abcdefghijklmnopqrstuvwxyz";
71  std::random_shuffle(digits.begin(), digits.end(), r);
72  std::cout << digits << "\n";
73  }
74  {
75  std::cout << "Estimate mean and variance of normal distribution: ";
76  double m = 0, s = 0;
77  int k = 0;
79  while (k < 10000) {
80  double x = n(r), m1 = m + (x - m)/++k;
81  s += (x - m) * (x - m1); m = m1;
82  }
83  std::cout << m << ", " << s/(k - 1) << "\n";
84  }
85  {
86  typedef float real;
87  enum { prec = 4 };
88  std::cout << "Some low precision reals (1/" << (1<<prec) << "):";
89  for (size_t i = 0; i < 5; ++i) std::cout << " " << r.Fixed<real, prec>();
90  std::cout << "\n";
91  }
92  std::cout << "Used " << r.Count() << " random numbers\n";
93  try {
94  // This throws an error if there's a problem
95  RandomLib::MRandom32::SelfTest();
96  std::cout << "Self test of " << RandomLib::MRandom32::Name()
97  << " passed\n";
98  RandomLib::MRandom64::SelfTest();
99  std::cout << "Self test of " << RandomLib::MRandom64::Name()
100  << " passed\n";
101  RandomLib::SRandom32::SelfTest();
102  std::cout << "Self test of " << RandomLib::SRandom32::Name()
103  << " passed\n";
104  RandomLib::SRandom64::SelfTest();
105  std::cout << "Self test of " << RandomLib::SRandom64::Name()
106  << " passed\n";
107  }
108  catch (std::out_of_range& e) {
109  std::cerr << "Self test FAILED: " << e.what() << "\n";
110  return 1;
111  }
112  return 0;
113 }
Header for NormalDistribution.
Header for RandomSelect.
bool Prob(NumericType p)
Generate random integers, reals, and booleans.
Random selection from a discrete set.
Header for Random, RandomGenerator.
std::bitset< nbits > Bits()
int main(int, char **)