LCOV - code coverage report
Current view: top level - lunchbox - rng.h (source / functions) Hit Total Coverage
Test: Lunchbox Lines: 10 11 90.9 %
Date: 2018-10-03 05:33:11 Functions: 11 11 100.0 %

          Line data    Source code
       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 <boost/noncopyable.hpp>
      22             : #include <limits>
      23             : #include <lunchbox/debug.h> // for LBASSERT
      24             : #include <lunchbox/init.h>  // friend function
      25             : #include <lunchbox/types.h>
      26             : 
      27             : namespace lunchbox
      28             : {
      29             : namespace detail
      30             : {
      31             : class RNG;
      32             : }
      33             : 
      34             : /**
      35             :  * A random number generator.
      36             :  *
      37             :  * Generates a set of random, or if not supported by the operating system,
      38             :  * pseudo-random numbers. Each instance creates its own series of numbers.
      39             :  * @deprecated Use Boost.Random
      40             :  *
      41             :  * Example: @include tests/rng.cpp
      42             :  */
      43             : class RNG : public boost::noncopyable
      44             : {
      45             : public:
      46             :     /** Construct a new random number generator. @version 1.0 */
      47             :     LUNCHBOX_API RNG();
      48             : 
      49             :     /** Destruct the random number generator. @version 1.0 */
      50             :     LUNCHBOX_API ~RNG();
      51             : 
      52             :     /**
      53             :      * Generate a random number.
      54             :      *
      55             :      * The returned number is between min..max for integer types, and
      56             :      * between 0..1 for floating-point types.
      57             :      * @return a random number.
      58             :      * @version 1.0
      59             :      */
      60             :     template <typename T>
      61      525080 :     T get()
      62             :     {
      63      100000 :         T value;
      64      525080 :         if (!_get(&value, sizeof(T)))
      65           0 :             return T();
      66      525080 :         return value;
      67             :     }
      68             : 
      69             : private:
      70             :     LUNCHBOX_API bool _get(void* data, size_t size);
      71             : };
      72             : 
      73             : template <>
      74          27 : inline float RNG::get()
      75             : {
      76             :     const float max_limits =
      77          27 :         static_cast<float>(std::numeric_limits<uint32_t>::max());
      78          27 :     return ((float)get<uint32_t>() / max_limits);
      79             : }
      80             : 
      81             : template <>
      82          75 : inline double RNG::get()
      83             : {
      84             :     const double max_limits =
      85          75 :         static_cast<double>(std::numeric_limits<uint64_t>::max());
      86          75 :     return ((double)get<uint64_t>() / max_limits);
      87             : }
      88             : 
      89             : template <>
      90             : inline bool RNG::get()
      91             : {
      92             :     return (get<uint32_t>() & 1);
      93             : }
      94             : }
      95             : #endif // LUNCHBOX_RNG_H

Generated by: LCOV version 1.11