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