RandomLib  1.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
RandomLambda.cpp
Go to the documentation of this file.
1 /**
2  * \file RandomLambda.cpp
3  * \brief Using the STL and lambda expressions with %RandomLib
4  *
5  * Compile/link with, e.g.,\n
6  * g++ -I../include -O2 -funroll-loops
7  * -o RandomLambda RandomLambda.cpp ../src/Random.cpp\n
8  * ./RandomLambda
9  *
10  * See \ref stl, for more information.
11  *
12  * Copyright (c) Charles Karney (2006-2011) <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 <vector>
19 #include <RandomLib/Random.hpp>
21 
22 #if (defined(__GXX_EXPERIMENTAL_CXX0X__) && __GNUC_MINOR__ >= 5) || \
23  (defined(_MSC_VER) && _MSC_VER >= 1600)
24 # define HAVE_LAMBDA 1
25 #if defined(_MSC_VER)
26 // Squelch warnings about nonstandard extensions
27 # pragma warning (disable: 4239)
28 #endif
29 #else
30 # define HAVE_LAMBDA 0
31 #endif
32 
33 /// \cond SKIP
34 #if !HAVE_LAMBDA
35 // Don't need to define these if we can use lambda expressions
36 template<typename RealType = double> class RandomNormal {
37 private:
40  const RealType _mean, _sigma;
41  RandomNormal& operator=(const RandomNormal&);
42 public:
43  RandomNormal(RandomLib::Random& r,
44  RealType mean = RealType(0), RealType sigma = RealType(1))
46  , _mean(mean), _sigma(sigma) {}
47  RealType operator()() { return _n(_r, _mean, _sigma); }
48 };
49 
50 template<typename IntType = unsigned> class RandomInt {
51 private:
53  RandomInt& operator=(const RandomInt&);
54 public:
55  RandomInt(RandomLib::Random& r)
56  : _r(r) {}
57  IntType operator()(IntType x) { return _r.Integer<IntType>(x); }
58 };
59 #endif
60 /// \endcond
61 
62 int main(int, char**) {
63  RandomLib::Random r; r.Reseed();
64 #if HAVE_LAMBDA
65  std::cout << "Illustrate calling STL routines with lambda expressions\n";
66 #else
67  std::cout << "Illustrate calling STL routines without lambda expressions\n";
68 #endif
69  std::cout << "Using " << r.Name() << "\n"
70  << "with seed " << r.SeedString() << "\n\n";
71 
72  std::vector<unsigned> c(10); // Fill with unsigned in [0, 2^32)
73 #if HAVE_LAMBDA
74  std::generate(c.begin(), c.end(),
75  [&r]() throw() -> unsigned { return r(); });
76 #else
77  std::generate<std::vector<unsigned>::iterator, RandomLib::Random&>
78  (c.begin(), c.end(), r);
79 #endif
80 
81  std::vector<double> b(10); // Fill with normal deviates
82 #if HAVE_LAMBDA
84  std::generate(b.begin(), b.end(),
85  [&r, &nf]() throw() -> double
86  { return nf(r,0.0,2.0); });
87 #else
88  std::generate(b.begin(), b.end(), RandomNormal<>(r,0.0,2.0));
89 #endif
90 
91  std::vector<int> a(20); // How to shuffle large vectors
92 #if HAVE_LAMBDA
93  int i = 0;
94  std::generate(a.begin(), a.end(),
95  [&i]() throw() -> int { return i++; });
96  std::random_shuffle(a.begin(), a.end(),
97  [&r](unsigned long long n) throw() -> unsigned long long
98  { return r.Integer<unsigned long long>(n); });
99 #else
100  for (size_t i = 0; i < a.size(); ++i) a[i] = int(i);
101  RandomInt<unsigned long long> shuffler(r);
102  std::random_shuffle(a.begin(), a.end(), shuffler);
103 #endif
104 
105  return 0;
106 }
Header for NormalDistribution.
Generate random integers, reals, and booleans.
Header for Random, RandomGenerator.
int main(int, char **)