Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 17, 2015, 6:28:25 PM (9 years ago)
Author:
muemart
Message:

Use new random number generation functions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/cpp11_v2/src/libraries/util/Math.h

    r10768 r10813  
    4646#include <cmath>
    4747#include <cstdlib>
     48#include <random>
    4849
    4950#include <OgreMath.h>
     
    252253    }
    253254
     255    namespace detail
     256    {
     257        /**
     258        Random number generator used for the functions below. Marked extern to only have one global instance.
     259        */
     260        _UtilExport extern std::mt19937 rngen;
     261    }
     262
     263    /**
     264    @brief Seeds the random number generator used for the functions below.
     265    */
     266    inline void rndseed(unsigned int seed)
     267    {
     268        detail::rngen.seed(seed);
     269    }
     270
     271    /**
     272    @brief Returns a random number between @a min and almost @a max: <tt>min <= rnd < max</tt>.
     273    @param min The minimum
     274    @param max The maximum
     275    */
     276    inline float rnd(float min, float max)
     277    {
     278        std::uniform_real_distribution<float> dist(min, max);
     279        return dist(detail::rngen);
     280    }
     281
    254282    /**
    255283        @brief Returns a random number between 0 and almost 1: <tt>0 <= rnd < 1</tt>.
     
    257285    inline float rnd()
    258286    {
    259         return rand() / (RAND_MAX + 1.0f);
     287        return rnd(0, 1);
    260288    }
    261289
     
    266294    inline float rnd(float max)
    267295    {
    268         return rnd() * max;
    269     }
    270 
    271     /**
    272         @brief Returns a random number between @a min and almost @a max: <tt>min <= rnd < max</tt>.
    273         @param min The minimum
    274         @param max The maximum
    275     */
    276     inline float rnd(float min, float max)
    277     {
    278         return rnd(max - min) + min;
     296        return rnd(0, max);
    279297    }
    280298
     
    284302    inline float rndsgn()
    285303    {
    286         return static_cast<float>((rand() & 0x2) - 1); // rand() & 0x2 is either 2 or 0
     304        std::uniform_int_distribution<> dist;
     305        return static_cast<float>((dist(detail::rngen) & 0x2) - 1); // dist(...) & 0x2 is either 2 or 0
    287306    }
    288307
Note: See TracChangeset for help on using the changeset viewer.