Random Number Values quite different based on declaration style

20 views
Skip to first unread message

Mas

unread,
Feb 20, 2019, 1:30:00 PM2/20/19
to ns-3-users
Hi All
I came across a protocol implementation by a researcher. He needs to call a function every second that gets values from uniform distribution in range (0, maxval.size() ) , the value of
maxval, which is a vector, also changes every second and it maximum value is 14. Therefore in every call to function per second the author declares random variable every time like

void getSLotNumber()
{

 
UniformVariable x(0, maxval.size() );
 val
=x.GetInteger();    
}

1. Is it correct to declare random variable again and again?
   I have used other technique knowing that maxval.size() is maximum 14, by declaring    Ptr<UniformRandomVariable> x initially as class object and then based on the size of current maxval i scale down the retrieved values in desired range (0, currentsize of vector). I did following:  
// declared variable in class
Ptr<UniformRandomVariable> x;
// in constructor initialized variable and assigned range
 x
= CreateObject<UniformRandomVariable> ();
double min = 0.0;
double max = 14.0;
x
->SetAttribute ("Min", DoubleValue (min));
x
->SetAttribute ("Max", DoubleValue (max));

// and called function every second this way
 
void getSLotNumber()
   
{

    y
=x->GetInteger);  
    val
=scaleWRT( maxval.size(),y ) // if current vector size is 5 then scales y value in
   
// range (0,5) instead of (0,14) using scaleWRT() function created

   
}
This way i dont need to redeclare random variable again and again due dynamic size of vairable (vector size)
But the pattern of random variable i got after scaling is quite different from what i get when i used already implemented logic with uniform variable declared every time. Any comments please.

I have also found in ns3 that below two segments of code produce quite differnt patterns


for(int a=0; a<20; a++){
 
UniformVariable x(0, 14 );
 val
=x.GetInteger();  
}

 
//vs
 
UniformVariable x(0, 14 );

for(int a=0; a<20; a++){
 val
=x.GetInteger();  }
Any Comments on this. For my network simulation both of these produced quite different results. Can any one tell whats the correct way to deal with random numbers?
Thanks

pdbarnes

unread,
Feb 25, 2019, 10:44:01 AM2/25/19
to ns-3-users
I’m not surprised they give different results: they use different RNG instances. The original implementation draws from a new stream each time, while yours draws from a single stream.

My preference would be to create the RNG once, then set its max value each call to the size of the vector. A single RNG will give repeatable results, independent of what else is happening or created in someone’ model.

Peter
Reply all
Reply to author
Forward
0 new messages