Question about generating different random numbers

21 views
Skip to first unread message

Doaa

unread,
Nov 11, 2015, 2:41:13 AM11/11/15
to ns-3-users
Hi all 

I want to generate 15 different random numbers. Below is what i have tried about, however I fount that there are numbers that are duplicated  SN1=[22 19 5 17 6 12 24 23 20 6 4 6 12 0 20]. Any suggestions would be highly appreciated. 

Ptr<UniformRandomVariable> rand = CreateObject<UniformRandomVariable>();
rand->SetAttribute( "Min", DoubleValue( 0 ) );
rand->SetAttribute( "Max", DoubleValue( 29 ) );
rand->SetAttribute( "Stream", IntegerValue( 1 ) );

for ( int k=0; k<15; k++ )
{
nodes[k]= rand->GetInteger();
cout << nodes[k] << " ";
}

pdbarnes

unread,
Nov 11, 2015, 10:14:08 AM11/11/15
to ns-3-users
You asked for 16 draws from a RNG uniform on the 30 integers [0,29]. There is almost surely going to be duplicates.

Perhaps you really want a (random) permutation of the sequence {0, 1...29}? Then take the first 16 entries?

There are many ways to do that. One easy way is to keep a vector< bool>(30) where you mark each value as it's selected. Then you draw in a loop until you draw a new value.

Peter

Reply all
Reply to author
Forward
0 new messages