RandomLib  1.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
RandomSave.cpp
Go to the documentation of this file.
1 /**
2  * \file RandomSave.cpp
3  * \brief Examples of saving and restore the state with %RandomLib
4  *
5  * Compile/link with, e.g.,\n
6  * g++ -DHAVE_BOOST_SERIALIZATION -I../include -O2 -funroll-loops
7  * -lboost_serialization-mt -o RandomSave RandomSave.cpp ../src/Random.cpp\n
8  * ./RandomSave
9  *
10  * See \ref save, for more information.
11  *
12  * Copyright (c) Charles Karney (2011) <charles@karney.com> and licensed under
13  * the MIT/X11 License. For more information, see
14  * http://randomlib.sourceforge.net/
15  **********************************************************************/
16 
17 #include <iostream>
18 #include <fstream>
19 #include <string>
20 #if HAVE_BOOST_SERIALIZATION
21 #if defined(_MSC_VER)
22 // Squelch warnings about argument conversion and unchecked parameters with
23 // boost serialization
24 #pragma warning (disable: 4244 4996)
25 #endif
26 #include <boost/archive/xml_iarchive.hpp>
27 #include <boost/archive/xml_oarchive.hpp>
28 #endif
29 #include <RandomLib/Random.hpp>
30 
31 int main(int, char**) {
32  int retval = 0;
33  long step = 1000000;
34  RandomLib::Random r; r.Reseed();
35  std::cout << "Using " << r.Name() << "\n"
36  << "with seed " << r.SeedString() << "\n";
37  r.StepCount(step); // use r
38  {
39  std::cout << "Test save and restore with copy constructor... ";
40  RandomLib::Random s(r); // copy r's state into s via copy constructor
41  r.StepCount(step); s.StepCount(step); // step both generators forward
42  std::cout << (r == s && r() == s()
43  ? "succeeded\n" : (retval = 1, "failed\n"));
44  }
45  {
46  std::cout << "Test save state via count... ";
47  long long savecount = r.Count(); // save the count
48  double d = r.Fixed(); // save a random result
49  r.StepCount(step); // step r forward
50  r.SetCount(savecount); // restore to saved count
51  std::cout << (d == r.Fixed()
52  ? "succeeded\n" : (retval = 1, "failed\n"));
53  }
54  {
55  std::cout << "Test save state via seed and count... ";
56  RandomLib::Random s(r.Seed()); // constructor with r's seed
57  s.SetCount(r.Count()); // advance s with r's count
58  r.StepCount(step); s.StepCount(step); // step both generators forward
59  std::cout << (r == s && r() == s()
60  ? "succeeded\n" : (retval = 1, "failed\n"));
61  }
62  {
63  std::cout << "Test save state to file in portable binary format... ";
64  {
65  std::ofstream f("rand.bin", std::ios::binary);
66  r.Save(f); // save r in binary mode to rand.bin
67  }
68  RandomLib::Random s(0);
69  {
70  std::ifstream f("rand.bin", std::ios::binary);
71  s.Load(f); // load saved state to s
72  }
73  r.StepCount(step); s.StepCount(step); // step both generators forward
74  std::cout << (r == s && r() == s()
75  ? "succeeded\n" : (retval = 1, "failed\n"));
76  }
77  {
78  std::cout << "Test save state to file in text format... ";
79  {
80  std::ofstream f("rand.txt");
81  f << "Random number state:\n" << r << "\n"; // save r with operator<<
82  }
83  RandomLib::Random s(0);
84  {
85  std::ifstream f("rand.txt");
86  std::string str;
87  std::getline(f, str); // skip over first line
88  f >> s; // read into s with operator>>
89  }
90  r.StepCount(step); s.StepCount(step); // step both generators forward
91  std::cout << (r == s && r() == s()
92  ? "succeeded\n" : (retval = 1, "failed\n"));
93  }
94  {
95 #if HAVE_BOOST_SERIALIZATION
96  std::cout << "Test save state to file in boost xml format... ";
97  {
98  std::ofstream f("rand.xml");
99  boost::archive::xml_oarchive oa(f); // set up an xml archive
100  oa << BOOST_SERIALIZATION_NVP(r); // save r to xml file rand.xml
101  }
102  RandomLib::Random s(0);
103  {
104  std::ifstream f("rand.xml");
105  boost::archive::xml_iarchive ia(f);
106  ia >> BOOST_SERIALIZATION_NVP(s); // load saved state to s
107  }
108  r.StepCount(step); s.StepCount(step); // step both generators forward
109  std::cout << (r == s && r() == s()
110  ? "succeeded\n" : (retval = 1, "failed\n"));
111 #else
112  std::cout << "Skipping boost tests\n";
113 #endif
114  }
115  return retval;
116 }
Generate random integers, reals, and booleans.
Header for Random, RandomGenerator.
int main(int, char **)
Definition: RandomSave.cpp:31