1,105,644 Community Members

Generate random numbers with no duplicates

Member Avatar
wonder_laptop
Junior Poster in Training
89 posts since Aug 2007
Reputation Points: -3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I created a simple Node.java class, where each Node instance has an
1-id variable of type int
2-Neighborlist of type Arraylist

I wrote a code that gives me a random network, made of 200 nodes, and each node can have up to 55 neighbors.

for(i=0; i <200; i++ ) // this is to generate 200 nodes
        {           
          Node n = new Node();
          n.id (i);
          for (j=0; j<10; j++) // to put an upper bound on the number of neighbors
             {
               idx= randomGenerator.nextInt(200);
               if (idx>=50 && idx!=i))   // 50 is a random number that ive chosen
                  {
                    // Add this node to the one hop neighbors of i
                    n.Neighborlist.add(idx);
                  }                        
              
              //Add node i to the network
              network_LN.add(n);
           }// end

The problem is that this code might produce nodes that have duplicates neighbors. Is there an efficient way to prevent duplicates in the neighbor list of each node ? i was thinking to loop over the neighbor list before adding an idx and make sure that it is not there before adding it...but is there any more efficient way to do that ?

Member Avatar
pritaeas
mod_pritaeas
11,315 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 156 [?]
Moderator
Featured
Sponsor
 
0
 

Generate a list of numbers, pick an index from this list, then delete it.

Member Avatar
wonder_laptop
Junior Poster in Training
89 posts since Aug 2007
Reputation Points: -3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

even if i generate a random list, i might get 2 or more elements that are the same...

Member Avatar
pritaeas
mod_pritaeas
11,315 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 156 [?]
Moderator
Featured
Sponsor
 
0
 

no, not a random list. Just a list of say 1..100 and pick at random from those.

Member Avatar
wonder_laptop
Junior Poster in Training
89 posts since Aug 2007
Reputation Points: -3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i dont see where i can integrate this in my code..

Member Avatar
mKorbel
Nearly a Posting Virtuoso
1,346 posts since Feb 2011
Reputation Points: 274 [?]
Q&As Helped to Solve: 277 [?]
Skill Endorsements: 14 [?]
 
0
 

or put that to some of Set because doesn't allows duplicates

Member Avatar
jon.kiparsky
Posting Virtuoso
1,837 posts since Jun 2010
Reputation Points: 326 [?]
Q&As Helped to Solve: 192 [?]
Skill Endorsements: 6 [?]
 
0
 

i dont see where i can integrate this in my code..

If I understand priteas, he's suggesting you make an arraylist of Integer, populate it with integers in the range you want, and remove(randomIndex) for your random numbers, randomIndex being a random int in the range (0..arraylist.size())
Instead of idx = (generated random number) , you'd have idx = arrayList.remove(generated random number (0..arraylist.size())) Simple.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: