RandHelper Class Reference

#include <RandHelper.h>


Detailed Description

Utility functions for using a global, resetable random number generator.

Definition at line 49 of file RandHelper.h.


Static Public Member Functions

template<class T>
static T getRandomFrom (const std::vector< T > &v)
 Returns a random element from the given vector.
static void initRandGlobal ()
 Reads the given random number options and initialises the random number geerator in accordance.
static void insertRandOptions ()
 Initialises the given options container with random number options.
static int rand (int minV, int maxV)
 Returns a random integer in [minV, maxV-1].
static int rand (int maxV)
 Returns a random integer in [0, maxV-1].
static size_t rand (size_t maxV)
 Returns a random integer in [0, maxV-1].
static SUMOReal rand (SUMOReal minV, SUMOReal maxV)
 Returns a random real number in [minV, maxV).
static SUMOReal rand (SUMOReal maxV)
 Returns a random real number in [0, maxV).
static SUMOReal rand ()
 Returns a random real number in [0, 1).
static SUMOReal randNorm (SUMOReal mean, SUMOReal variance)
 Access to a random number from a normal distribution.

Static Protected Attributes

static MTRand myRandomNumberGenerator
 the random number generator to use

Member Function Documentation

template<class T>
static T RandHelper::getRandomFrom ( const std::vector< T > &  v  )  [inline, static]

Returns a random element from the given vector.

Definition at line 95 of file RandHelper.h.

References rand().

Referenced by MSEdge::getDepartLane(), RONet::getRandomDestination(), and RONet::getRandomSource().

00095                                          {
00096         return v[rand(v.size())];
00097     }

void RandHelper::initRandGlobal (  )  [static]

Reads the given random number options and initialises the random number geerator in accordance.

Definition at line 64 of file RandHelper.cpp.

References OptionsCont::getBool(), OptionsCont::getInt(), OptionsCont::getOptions(), myRandomNumberGenerator, and MTRand::seed().

Referenced by main(), and GUILoadThread::run().

00064                            {
00065     OptionsCont &oc = OptionsCont::getOptions();
00066     if (oc.getBool("abs-rand")) {
00067         myRandomNumberGenerator.seed();
00068     } else {
00069         myRandomNumberGenerator.seed(oc.getInt("srand"));
00070     }
00071 }

void RandHelper::insertRandOptions (  )  [static]

Initialises the given options container with random number options.

Definition at line 50 of file RandHelper.cpp.

References OptionsCont::addDescription(), OptionsCont::addOptionSubTopic(), OptionsCont::doRegister(), and OptionsCont::getOptions().

Referenced by ROJTRFrame::fillOptions(), RODUAFrame::fillOptions(), RODFFrame::fillOptions(), NIOptionsIO::fillOptions(), fillOptions(), and MSFrame::fillOptions().

00050                               {
00051     OptionsCont &oc = OptionsCont::getOptions();
00052     // registers random number options
00053     oc.addOptionSubTopic("Random Number");
00054 
00055     oc.doRegister("srand", new Option_Integer(23423));
00056     oc.addDescription("srand", "Random Number", "Initialises the random number generator with the given value");
00057 
00058     oc.doRegister("abs-rand", new Option_Bool(false));
00059     oc.addDescription("abs-rand", "Random Number", "Initialises the random number generator with the current system time");
00060 }

static int RandHelper::rand ( int  minV,
int  maxV 
) [inline, static]

Returns a random integer in [minV, maxV-1].

Definition at line 83 of file RandHelper.h.

References rand().

00083                                                {
00084         return minV + rand(maxV - minV);
00085     }

static int RandHelper::rand ( int  maxV  )  [inline, static]

Returns a random integer in [0, maxV-1].

Definition at line 78 of file RandHelper.h.

References myRandomNumberGenerator, and MTRand::randInt().

00078                                      {
00079         return (int) RandHelper::myRandomNumberGenerator.randInt((MTRand::uint32)(maxV-1));
00080     }

static size_t RandHelper::rand ( size_t  maxV  )  [inline, static]

Returns a random integer in [0, maxV-1].

Definition at line 73 of file RandHelper.h.

References myRandomNumberGenerator, and MTRand::randInt().

00073                                            {
00074         return (size_t) RandHelper::myRandomNumberGenerator.randInt((MTRand::uint32)(maxV-1));
00075     }

static SUMOReal RandHelper::rand ( SUMOReal  minV,
SUMOReal  maxV 
) [inline, static]

Returns a random real number in [minV, maxV).

Definition at line 68 of file RandHelper.h.

References rand().

00068                                                               {
00069         return minV + (maxV - minV) * rand();
00070     }

static SUMOReal RandHelper::rand ( SUMOReal  maxV  )  [inline, static]

Returns a random real number in [0, maxV).

Definition at line 63 of file RandHelper.h.

References rand().

00063                                                {
00064         return maxV * rand();
00065     }

static SUMOReal RandHelper::rand (  )  [inline, static]

static SUMOReal RandHelper::randNorm ( SUMOReal  mean,
SUMOReal  variance 
) [inline, static]

Access to a random number from a normal distribution.

Definition at line 88 of file RandHelper.h.

References myRandomNumberGenerator, MTRand::randNorm(), and SUMOReal.

Referenced by MSVehicleType::getMaxSpeedWithDeviation().

00088                                                                       {
00089         return (SUMOReal) RandHelper::myRandomNumberGenerator.randNorm(mean, variance);
00090     }


Field Documentation

the random number generator to use

Definition at line 101 of file RandHelper.h.

Referenced by initRandGlobal(), rand(), and randNorm().


The documentation for this class was generated from the following files:

Generated on Wed May 5 00:06:59 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6