Lunchbox  1.10.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
rng.h
1 
2 /* Copyright (c) 2007-2014, Stefan Eilemann <eile@equalizergraphics.com>
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License version 2.1 as published
6  * by the Free Software Foundation.
7  *
8  * This library is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11  * details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16  */
17 
18 #ifndef LUNCHBOX_RNG_H
19 #define LUNCHBOX_RNG_H
20 
21 #include <lunchbox/debug.h> // for LBASSERT
22 #include <lunchbox/init.h> // friend function
23 #include <lunchbox/types.h>
24 #include <boost/noncopyable.hpp>
25 #include <limits>
26 
27 namespace lunchbox
28 {
29 namespace detail { class RNG; }
30 
40 class RNG : public boost::noncopyable
41 {
42 public:
44  LUNCHBOX_API RNG();
45 
47  LUNCHBOX_API ~RNG();
48 
50  LUNCHBOX_API void reseed();
51 
60  template< typename T > T get()
61  {
62  T value;
63  if( !_get( &value, sizeof( T )))
64  return T();
65  return value;
66  }
67 
68 private:
69  detail::RNG* const _impl;
70 
71  static LUNCHBOX_API bool _init();
72  static void _exit();
73  friend LUNCHBOX_API bool init( const int argc, char** argv );
74  LUNCHBOX_API bool _get( void* data, size_t size );
75 }; // LB_DEPRECATED;
76 
77 template<> inline float RNG::get()
78 {
79  const float max_limits =
80  static_cast< float >( std::numeric_limits< uint32_t >::max( ));
81  return ((float)get< uint32_t >() / max_limits);
82 }
83 
84 template<> inline double RNG::get()
85 {
86  const double max_limits =
87  static_cast< double >( std::numeric_limits< uint64_t >::max( ));
88  return ((double)get< uint64_t >() / max_limits);
89 }
90 
91 template<> inline bool RNG::get()
92 {
93  return ( get< uint32_t >() & 1 );
94 }
95 }
96 #endif // LUNCHBOX_RNG_H
Basic type definitions not provided by the operating system.
A random number generator.
Definition: rng.h:40
LUNCHBOX_API RNG()
Construct a new random number generator.
friend LUNCHBOX_API bool init(const int argc, char **argv)
Initialize the Lunchbox base classes.
LUNCHBOX_API void reseed()
Re-initialize the seed value for pseudo RNG's.
LUNCHBOX_API ~RNG()
Destruct the random number generator.
T get()
Generate a random number.
Definition: rng.h:60