The basic RNG for NS-3.Note: The underlying random number generation method used by NS-3 is the RngStream code by Pierre L'Ecuyer at the University of Montreal. More...
#include <random-variable.h>
Public Member Functions | |
RandomVariable () | |
RandomVariable (const RandomVariable &o) | |
RandomVariable & | operator= (const RandomVariable &o) |
~RandomVariable () | |
double | GetValue (void) const |
Returns a random double from the underlying distribution. | |
uint32_t | GetInteger (void) const |
Returns a random integer integer from the underlying distribution. | |
void | GetSeed (uint32_t seed[6]) const |
Get the internal state of the RNG. | |
Static Public Member Functions | |
static void | UseDevRandom (bool udr=true) |
Set seeding behavior. | |
static void | UseGlobalSeed (uint32_t s0, uint32_t s1, uint32_t s2, uint32_t s3, uint32_t s4, uint32_t s5) |
Use the global seed to force precisely reproducible results. | |
static void | SetRunNumber (uint32_t n) |
Set the run number of this simulation. | |
Protected Member Functions | |
RandomVariable (const RandomVariableBase &variable) | |
RandomVariableBase * | Peek (void) const |
Private Attributes | |
RandomVariableBase * | m_variable |
Friends | |
std::ostream & | operator<< (std::ostream &os, const RandomVariable &var) |
std::istream & | operator>> (std::istream &os, RandomVariable &var) |
The basic RNG for NS-3.
Note: The underlying random number generation method used by NS-3 is the RngStream code by Pierre L'Ecuyer at the University of Montreal.
NS-3 has a rich set of random number generators. Class RandomVariable defines the base class functionalty required for all random number generators. By default, the underlying generator is seeded with the time of day, and then deterministically creates a sequence of seeds for each subsequent generator that is created. The rest of the documentation outlines how to change this behavior.
Definition at line 57 of file random-variable.h.
ns3::RandomVariable::RandomVariable | ( | ) |
Definition at line 245 of file random-variable.cc.
ns3::RandomVariable::RandomVariable | ( | const RandomVariable & | o | ) |
Definition at line 248 of file random-variable.cc.
ns3::RandomVariable::~RandomVariable | ( | ) |
Definition at line 265 of file random-variable.cc.
References m_variable.
ns3::RandomVariable::RandomVariable | ( | const RandomVariableBase & | variable | ) | [protected] |
Definition at line 251 of file random-variable.cc.
uint32_t ns3::RandomVariable::GetInteger | ( | void | ) | const |
Returns a random integer integer from the underlying distribution.
Definition at line 276 of file random-variable.cc.
References ns3::RandomVariableBase::GetInteger(), and m_variable.
void ns3::RandomVariable::GetSeed | ( | uint32_t | seed[6] | ) | const |
Get the internal state of the RNG.
This function is for power users who understand the inner workings of the underlying RngStream method used. It returns the internal state of the RNG via the input parameter.
seed | Output parameter; gets overwritten with the internal state of of the RNG. |
Definition at line 281 of file random-variable.cc.
References ns3::RandomVariableBase::GetSeed(), and m_variable.
double ns3::RandomVariable::GetValue | ( | void | ) | const |
Returns a random double from the underlying distribution.
Definition at line 270 of file random-variable.cc.
References ns3::RandomVariableBase::GetValue(), and m_variable.
Referenced by ns3::RandomDirection2dMobilityModel::BeginPause(), ns3::RandomWaypointMobilityModel::BeginWalk(), ns3::RandomPropagationLossModel::DoCalcRxPower(), ns3::RateErrorModel::DoCorruptBit(), ns3::RateErrorModel::DoCorruptByte(), ns3::RateErrorModel::DoCorruptPkt(), ns3::YansWifiPhy::EndReceivePacket(), ns3::RandomPropagationDelayModel::GetDelay(), ns3::RandomDiscPositionAllocator::GetNext(), ns3::RandomRectanglePositionAllocator::GetNext(), ns3::ErlangVariableImpl::GetSingleValue(), ns3::ErlangVariableImpl::GetValue(), ns3::GammaVariableImpl::GetValue(), ns3::SequentialVariableImpl::GetValue(), ns3::RandomVariableTest::RunTests(), ns3::BufferTest::RunTests(), ns3::OnOffApplication::ScheduleNextTx(), ns3::OnOffApplication::ScheduleStartEvent(), ns3::OnOffApplication::ScheduleStopEvent(), ns3::RandomDirection2dMobilityModel::SetDirectionAndSpeed(), ns3::Application::Start(), ns3::RandomWaypointMobilityModel::Start(), ns3::RandomWalk2dMobilityModel::Start(), and ns3::Application::Stop().
RandomVariable & ns3::RandomVariable::operator= | ( | const RandomVariable & | o | ) |
Definition at line 255 of file random-variable.cc.
References ns3::RandomVariableBase::Copy(), and m_variable.
RandomVariableBase * ns3::RandomVariable::Peek | ( | void | ) | const [protected] |
Definition at line 302 of file random-variable.cc.
References m_variable.
Referenced by ns3::EmpiricalVariable::CDF().
void ns3::RandomVariable::SetRunNumber | ( | uint32_t | n | ) | [static] |
Set the run number of this simulation.
These RNGs have the ability to give independent sets of trials for a fixed global seed. For example, suppose one sets up a simulation with RandomVariables with a given global seed. Suppose the user wanted to retry the same simulation with different random values for validity, statistical rigor, etc. The user could either change the global seed and re-run the simulation, or could use this facility to increment all of the RNGs to a next substream state. This predictably advances the internal state of all RandomVariables n steps. This should be called immediately after the global seed is set, and before the creation of any RandomVariables. For example:
RandomVariable::UseGlobalSeed(1,2,3,4,5,6); int N = atol(argv[1]); //read in run number from command line RandomVariable::SetRunNumber(N); UniformVariable x(0,10); ExponentialVariable y(2902);
In this example, N could successivly be equal to 1,2,3, etc. and the user would continue to get independent runs out of the single simulation. For this simple example, the following might work:
./simulation 0 ...Results for run 0:... ./simulation 1 ...Results for run 1:...
Definition at line 297 of file random-variable.cc.
void ns3::RandomVariable::UseDevRandom | ( | bool | udr = true |
) | [static] |
Set seeding behavior.
Specify whether the POSIX device /dev/random is to be used for seeding. When this is used, the underlying generator is seeded with data from /dev/random instead of being seeded based upon the time of day. For this to be effective, it must be called before the creation of the first instance of a RandomVariable or subclass. Example:
RandomVariable::UseDevRandom(); UniformVariable x(2,3); //these are seeded randomly ExponentialVariable y(120); //etc
udr | True if /dev/random desired. |
Definition at line 286 of file random-variable.cc.
void ns3::RandomVariable::UseGlobalSeed | ( | uint32_t | s0, | |
uint32_t | s1, | |||
uint32_t | s2, | |||
uint32_t | s3, | |||
uint32_t | s4, | |||
uint32_t | s5 | |||
) | [static] |
Use the global seed to force precisely reproducible results.
It is often desirable to create a simulation that uses random numbers, while at the same time is completely reproducible. Specifying this set of six random seeds initializes the random number generator with the specified seed. Once this is set, all generators will produce fixed output from run to run. This is because each time a new generator is created, the underlying RngStream deterministically creates a new seed based upon the old one, hence a "stream" of RNGs. Example:
RandomVariable::UseGlobalSeed(...); UniformVariable x(2,3); //these will give the same output everytime ExponentialVariable y(120); //as long as the seed stays the same
s0 | ||
s1 | ||
s2 | ||
s3 | ||
s4 | ||
s5 |
Definition at line 291 of file random-variable.cc.
std::ostream& operator<< | ( | std::ostream & | os, | |
const RandomVariable & | var | |||
) | [friend] |
std::istream& operator>> | ( | std::istream & | os, | |
RandomVariable & | var | |||
) | [friend] |
Definition at line 170 of file random-variable.h.
Referenced by GetInteger(), GetSeed(), GetValue(), operator=(), Peek(), and ~RandomVariable().