RandomLib  1.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
Random.hpp
Go to the documentation of this file.
1 /**
2  * \file Random.hpp
3  * \brief Header for Random, RandomGenerator.
4  *
5  * This loads up the header for RandomCanonical, RandomEngine, etc., to
6  * provide access to random integers of various sizes, random reals with
7  * various precisions, a random probability, etc.
8  *
9  * Copyright (c) Charles Karney (2006-2011) <charles@karney.com> and licensed
10  * under the MIT/X11 License. For more information, see
11  * http://randomlib.sourceforge.net/
12  **********************************************************************/
13 
14 #if !defined(RANDOMLIB_RANDOM_HPP)
15 #define RANDOMLIB_RANDOM_HPP 1
16 
17 #include <RandomLib/Config.h>
18 
19 #if defined(_MSC_VER)
20 typedef unsigned uint32_t;
21 typedef unsigned long long uint64_t;
22 #else
23 #include <stdint.h>
24 #endif
25 
26 /**
27  * Use table, Power2::power2, for pow2? This isn't necessary with g++ 4.0
28  * because calls to std::pow are optimized. g++ 4.1 seems to have lost this
29  * capability though! And it's back in g++ 4.4. So, for simplicity, assume
30  * that all "current" versions of g++ perform the optimization.
31  **********************************************************************/
32 #if !defined(RANDOMLIB_POWERTABLE)
33 #if defined(__GNUC__)
34 #define RANDOMLIB_POWERTABLE 0
35 #else
36 // otherwise use a lookup table
37 #define RANDOMLIB_POWERTABLE 1
38 #endif
39 #endif
40 
41 #if !HAVE_LONG_DOUBLE || defined(_MSC_VER)
42 #define RANDOMLIB_LONGDOUBLEPREC 53
43 #elif defined(__sparc)
44 #define RANDOMLIB_LONGDOUBLEPREC 113
45 #else
46 /**
47  * The precision of long doubles, used for sizing Power2::power2. 64 on
48  * Linux/Intel, 106 on MaxOS/PowerPC
49  **********************************************************************/
50 #define RANDOMLIB_LONGDOUBLEPREC __LDBL_MANT_DIG__
51 #endif
52 
53 /**
54  * A compile-time assert. Use C++11 static_assert, if available.
55  **********************************************************************/
56 #if !defined(STATIC_ASSERT)
57 # if __cplusplus >= 201103
58 # define STATIC_ASSERT static_assert
59 # elif defined(__GXX_EXPERIMENTAL_CXX0X__)
60 # define STATIC_ASSERT static_assert
61 # elif defined(_MSC_VER) && _MSC_VER >= 1600
62 # define STATIC_ASSERT static_assert
63 # else
64 # define STATIC_ASSERT(cond,reason) \
65  { enum{ STATIC_ASSERT_ENUM = 1/int(cond) }; }
66 # endif
67 #endif
68 
69 /**
70  * Are denormalized reals of type RealType supported?
71  **********************************************************************/
72 #define RANDOMLIB_HASDENORM(RealType) 1
73 
74 #if defined(_MSC_VER) && defined(RANDOMLIB_SHARED_LIB) && RANDOMLIB_SHARED_LIB
75 # if RANDOMLIB_SHARED_LIB > 1
76 # error RANDOMLIB_SHARED_LIB must be 0 or 1
77 # elif defined(RandomLib_EXPORTS)
78 # define RANDOMLIB_EXPORT __declspec(dllexport)
79 # else
80 # define RANDOMLIB_EXPORT __declspec(dllimport)
81 # endif
82 #else
83 # define RANDOMLIB_EXPORT
84 #endif
85 
86 #include <stdexcept>
87 
88 /**
89  * \brief Namespace for %RandomLib
90  *
91  * All of %RandomLib is defined within the RandomLib namespace. In addtiion
92  * all the header files are included via %RandomLib/filename. This minimizes
93  * the likelihood of conflicts with other packages.
94  **********************************************************************/
95 namespace RandomLib {
96 
97  /**
98  * \brief Exception handling for %RandomLib
99  *
100  * A class to handle exceptions. It's derived from std::runtime_error so it
101  * can be caught by the usual catch clauses.
102  **********************************************************************/
103  class RandomErr : public std::runtime_error {
104  public:
105 
106  /**
107  * Constructor
108  *
109  * @param[in] msg a string message, which is accessible in the catch
110  * clause, via what().
111  **********************************************************************/
112  RandomErr(const std::string& msg) : std::runtime_error(msg) {}
113  };
114 
115 } // namespace RandomLib
116 
118 
119 #if !defined(RANDOMLIB_BUILDING_LIBRARY)
120 
121 namespace RandomLib {
122 
123 #if !defined(RANDOMLIB_DEFAULT_GENERATOR)
124 #define RANDOMLIB_DEFAULT_GENERATOR SRandomGenerator32
125 #endif
126 
127  /**
128  * Point Random to one of a specific MT19937 generators.
129  **********************************************************************/
131 
132  /**
133  * Hook Random to RandomGenerator
134  **********************************************************************/
136 
137 } // namespace RandomLib
138 
139 #endif // !defined(RANDOMLIB_BUILDING_LIBRARY)
140 
141 #endif // RANDOMLIB_RANDOM_HPP
RandomErr(const std::string &msg)
Definition: Random.hpp:112
Generate random integers, reals, and booleans.
#define RANDOMLIB_DEFAULT_GENERATOR
Definition: Random.hpp:124
Exception handling for RandomLib.
Definition: Random.hpp:103
RANDOMLIB_DEFAULT_GENERATOR RandomGenerator
Definition: Random.hpp:130
RandomCanonical< RandomGenerator > Random
Definition: Random.hpp:135
Header for RandomCanonical.