Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 10813


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

Use new random number generation functions

Location:
code/branches/cpp11_v2/src/libraries
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/branches/cpp11_v2/src/libraries/core/CoreConfig.cc

    r10765 r10813  
    3535#include "core/Language.h"
    3636#include "core/ApplicationPaths.h"
     37
     38#include <random>
    3739
    3840namespace orxonox
     
    129131        if (!bInitialized && this->bInitRandomNumberGenerator_)
    130132        {
    131             srand(static_cast<unsigned int>(time(0)));
     133            std::random_device rnddev;
     134            rndseed(rnddev());
     135            //Keep the old seeding around because people will probably still use the old functions
     136            srand(rnddev());
    132137            rand();
    133138            bInitialized = true;
  • code/branches/cpp11_v2/src/libraries/util/Math.cc

    r10630 r10813  
    371371    }
    372372
     373
     374    namespace detail
     375    {
     376        std::mt19937 rngen;
     377    }
     378
    373379    /**
    374380        @brief Returns a unique number. This function will never return the same value twice.
  • 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.