RandomExample.cpp
Go to the documentation of this file.
00001 /**
00002  * \file RandomExample.cpp
00003  * \brief Example of use of RandomSeed, MT19937, Random, etc.
00004  *
00005  * Compile/link with, e.g.,\n
00006  * g++ -I.. -O2 -funroll-loops -o RandomExample RandomExample.cpp Random.cpp\n
00007  * ./RandomExample
00008  *
00009  * Written by Charles Karney <charles@karney.com> and licensed under the LGPL.
00010  * For more information, see http://randomlib.sourceforge.net/
00011  **********************************************************************/
00012 #include <iostream>
00013 #include <vector>
00014 #include <string>
00015 #include <algorithm>
00016 #include "RandomLib/Random.hpp"
00017 #include "RandomLib/NormalDistribution.hpp"
00018 #include "RandomLib/RandomSelect.hpp"
00019 
00020 #define RANDOMEXAMPLE_CPP "$Id: RandomExample.cpp 6723 2010-01-11 14:20:10Z ckarney $"
00021 RCSID_DECL(RANDOMEXAMPLE_CPP)
00022 RCSID_DECL(NORMALDISTRIBUTION_HPP)
00023 RCSID_DECL(RANDOMSELECT_HPP)
00024 
00025 int main() {
00026   RandomLib::Random r;          // r created with random seed
00027   std::cout << "Using " << r.Name() << "\n"
00028             << "with seed " << r.SeedString() << std::endl;
00029   {
00030     std::cout << "Estimate pi = ";
00031     size_t in = 0, num = 10000;
00032     for (size_t i = 0; i < num; ++i) {
00033       const double x = r.FixedS(); // r.FixedS() is in the interval (-1/2, 1/2)
00034       const double y = r.FixedS();
00035       if (x * x + y * y < 0.25)
00036         ++in;                   // Inside the circle
00037     }
00038     std::cout << (4.0 * in) / num << std::endl;
00039   }
00040   {
00041     std::cout << "Tossing a coin 20 times: ";
00042     for (size_t i = 0; i < 20; ++i)
00043       std::cout << (r.Boolean() ? "H" : "T");
00044     std::cout << std::endl;
00045   }
00046   {
00047     std::cout << "Throwing a pair of dice 15 times:";
00048     for (size_t i = 0; i < 15; ++i)
00049       std::cout << " " << r.IntegerC(1,6) + r.IntegerC(1,6);
00050     std::cout << std::endl;
00051   }
00052   {
00053     // Weights for throwing a pair of dice
00054     unsigned w[] = { 0, 0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 };
00055     // Initialize selection
00056     RandomLib::RandomSelect<unsigned> sel(w, w + sizeof(w)/sizeof(unsigned));
00057 
00058     std::cout << "Another 20 throws:";
00059     for (size_t i = 0; i < 20; ++i)
00060       std::cout << " " << sel(r);
00061     std::cout << std::endl;
00062   }
00063   {
00064     std::cout << "Draw balls from urn containing 5 red and 5 white balls: ";
00065     int t = 10, w = 5;
00066     while (t)
00067       std::cout << (r.Prob(w, t--) ? w--, "W" : "R");
00068     std::cout << std::endl;
00069   }
00070   {
00071     std::cout << "Shuffling the digits 0..9: ";
00072     std::string digits = "0123456789";
00073     std::random_shuffle(digits.begin(), digits.end(), r);
00074     std::cout << digits << std::endl;
00075   }
00076   {
00077     std::cout << "Estimate mean and variance of normal distribution: ";
00078     double m = 0;
00079     double s = 0;
00080     int k = 0;
00081     RandomLib::NormalDistribution<> n;
00082     while (k++ < 10000) {
00083       double x = n(r);
00084       double m1 = m + (x - m)/k;
00085       s += (x - m) * (x - m1);
00086       m = m1;
00087     }
00088     std::cout << m << ", " << s/(k - 1) << std::endl;
00089   }
00090   {
00091     typedef float real;
00092     enum { prec = 4 };
00093     std::cout << "Some low precision reals (1/"
00094               << (1<<prec) << "): ";
00095     for (size_t i = 0; i < 5; ++i)
00096       std::cout << " " << r.Fixed<real, prec>();
00097     std::cout << std::endl;
00098   }
00099   std::cout << "Used " << r.Count() << " random numbers" << std::endl;
00100   try {
00101     // This throws an error if there's a problem
00102     RandomLib::MRandomGenerator32::SelfTest();
00103     std::cout << "Self test of " << RandomLib::MRandomGenerator32::Name()
00104               << " passed" << std::endl;
00105     RandomLib::MRandomGenerator64::SelfTest();
00106     std::cout << "Self test of " << RandomLib::MRandomGenerator64::Name()
00107               << " passed" << std::endl;
00108     RandomLib::SRandomGenerator32::SelfTest();
00109     std::cout << "Self test of " << RandomLib::SRandomGenerator32::Name()
00110               << " passed" << std::endl;
00111     RandomLib::SRandomGenerator64::SelfTest();
00112     std::cout << "Self test of " << RandomLib::SRandomGenerator64::Name()
00113               << " passed" << std::endl;
00114   }
00115   catch (std::out_of_range& e) {
00116     std::cerr << "Self test FAILED: " << e.what() << std::endl;
00117     return 1;
00118   }
00119   return 0;
00120 }