random number generation

329 views
Skip to first unread message

Amir Reda

unread,
Dec 4, 2014, 2:36:12 PM12/4/14
to ns-3-...@googlegroups.com
dear all
i'm trying to generate a random number which is an attribute for each node  in the network using function GenerateRandomNumber() which is used on the constructor of the routing protocol to generate a random number and set the attribute uint32_t m_myRandomNo.

  uint32_t
  AeroRoutingProtocol :: GenerateRandomNumber ()
  {
    uint32_t rand = m_ipv4->GetObject<Node> ()->GetId ();
    m_myRandomNo = UniformRandomVariable().GetInteger (rand,(rand+1000));
    NS_LOG_FUNCTION (this << "the created random no is " << m_myRandomNo);
    return m_myRandomNo;

  }

i got this error during running a script

Program received signal SIGSEGV, Segmentation fault.
0xb5fc05ae in ns3::RngStream::RandU01 (this=0x0)
    at ../src/core/model/rng-stream.cc:258
258      p1 = a12 * m_currentState[1] - a13n * m_currentState[0];
(gdb) backtrace
#0  0xb5fc05ae in ns3::RngStream::RandU01 (this=0x0)
    at ../src/core/model/rng-stream.cc:258
#1  0xb5fadc1b in ns3::UniformRandomVariable::GetValue (this=0xbfffe320,
    min=0, max=1001) at ../src/core/model/random-variable-stream.cc:164
#2  0xb5fadd4a in ns3::UniformRandomVariable::GetInteger (this=0xbfffe320,
    min=0, max=1000) at ../src/core/model/random-variable-stream.cc:175
#3  0xb7b0eb98 in ns3::AeroRP::AeroRoutingProtocol::GenerateRandomNumber (
    this=0x811fcb8) at ../src/aerorp/model/aerorp-routing-protocol.cc:1420
#4  0xb7b0e096 in ns3::AeroRP::AeroRoutingProtocol::AuthenticationIntialize (
    this=0x811fcb8) at ../src/aerorp/model/aerorp-routing-protocol.cc:1337
#5  0xb7b08446 in ns3::AeroRP::AeroRoutingProtocol::Start (this=0x811fcb8)
    at ../src/aerorp/model/aerorp-routing-protocol.cc:946
#6  0x08057930 in AeroRPSimulation::CaseRun (this=0xbfffe954, nWifis=60,
    nSinks=1, nodeSpeed=1200, totalTime=1500, dataStart=100,
    printRoutes=false, rate=..., CSVfileName=...)
    at ../scratch/60nodetest.cc:350
#7  0x08056566 in main (argc=1, argv=0xbfffec84)
    at ../scratch/60nodetest.cc:223

i have reviewed the documentation i didn't made something wrong (this is what i thought) that the function has a null pointer

thanks allot for help
--
Warmest regards and best wishes for a good health,urs sincerely
mero

Tom Henderson

unread,
Dec 4, 2014, 3:04:33 PM12/4/14
to ns-3-...@googlegroups.com
http://www.nsnam.org/docs/manual/html/random-variables.html#background

You can see here that the recommended way of creating a random variable
is to use CreateObject, as shown below.

Ptr<UniformRandomVariable> x = CreateObject<UniformRandomVariable> ();

So, in your code, I recommend that you create a member variable
Ptr<UniformRandomVariable> m_randomVariable and assign it such as in the
constructor.

m_randomVariable = CreateObject<UniformRandomVariable> ();

then your code would look like:

uint32_t
AeroRoutingProtocol :: GenerateRandomNumber ()
{
uint32_t rand = m_ipv4->GetObject<Node> ()->GetId ();
m_myRandomNo = m_randomVariable->GetInteger (rand,(rand+1000));
NS_LOG_FUNCTION (this << "the created random no is " << m_myRandomNo);
return m_myRandomNo;
}

The reason that your original code did not work is that the random
variable relies on the attribute construction code being called, which
happens when you use the templated CreateObject method to create it.
When you create a temporary object as you did, without calling
CreateObject(), then only the class constructor is called and not also
the attribute construction code, and a null m_rng pointer within that
object results.

I will update the manual chapter to emphasize this.

- Tom
Reply all
Reply to author
Forward
0 new messages