Question

How do I generate random numbers say in range 0 - 100 when the user specifies the number of vertices needed. I implemented that for edges below but similar thing is required for vertices. How can it fit into the existing structure below? :

int main(int argc, char *argv[]) {
// input number of vertices and edges
  int V, E;
  cin >> V >> E;

// check for maximum number of possible edges (disallow anti-parallel arcs)
  if (E > V*(V-1)/2) {
      E = V*(V-1)/2;  
  }

// construct random weighted graph
vector<vector<int> > G(V, vector<int>(V, 0));
for (int i = 0; i < E; i++)
   while (true) {
      const int u = rand() % V,
                v = rand() % V;

   // no self loops
   if (u != v)
       // disallow anti-parallel edges and overwriting existing arcs
       if (0 == G[v][u] && 0 == G[u][v]) {
           G[u][v] = rand() % 100;
           break;
       }
    }


 // union-find sets are implemented inside the arrays
 vector<int> parent(V, 0), depth(V, 0);

 // at the start, all nodes point to themselves
 for (int i = 0; i < V; i++) {
    parent[i] = i;
    depth[i] = 0;
 }

 // load all edges into priority queue, smallest weight on top of max-heap
 priority_queue<PairPQ<int, int> > PQ;
 for (int u = 0; u < V; u++)
    for (int v = 0; v < V; v++)
       if (G[u][v])
          PQ.push(PairPQ<int, int>(u, v, -G[u][v]));

 // keep track of MST edges
 set<int> MSTedges;

 // loop over all edges in ascending order of weight
 while (!PQ.empty()) {
    // no need for lazy decrease key idiom here!
    const int u = PQ.top().item().first,
              v = PQ.top().item().second;
   // (don't care about rank/weight, note too that direction does not matter)
   PQ.pop();

   // check for edge between disjoint sets
   if (find_set(u, parent) != find_set(v, parent)) {
      MSTedges.insert(u * V + v);
      union_set(u, v, parent, depth);
   }
  }

Where the random vertices are needed:

  // printing MST graph in GraphViz format
  *cout << endl 
       << "digraph G {" << endl
       << "node [shape=circle]" << endl;
 for (int i = 0; i < V; i++)
    for (int j = i+1;j < V; j++) {
       const int w = max(G[i][j], G[j][i]);
       if (w) {
            cout << "  " << j <<" -> " << i 
                 << " [label=\"" << w << "\"";
       if (MSTedges.count(i * V + j) || MSTedges.count(j * V + i))
           cout << ",color=red";
           cout << "];" << endl;
      }

    }
   cout << "}" << endl;
   exit(0);*
 }

Random numbers to be generated are the nodes Random numbers to be generated are the nodes.

Was it helpful?

Solution

This code should roughly follow the format you need, I provide both a C++11 solution using the random header and uniform_int_distribution with live example which should be preferred if possible::

#include <iostream>
#include <random>
#include <cstdlib>

int main()
{
    std::random_device rd;

    std::mt19937 e2(rd());

    int V = 10 ;

    std::uniform_int_distribution<int> dist1(0, V);


    for (int i = 0; i < V; ++i)
    {
        for (int k = i+1; k < V; ++k)
        {
            int index1 = dist1(e2) ;
            int index2 = dist1(e2) ;
            std::cout << "( " <<  index1 << " , " << index2 << ") " ;
        }
        std::cout << std::endl ;
    }
    std::cout << std::endl ;
}

I also provide code using rand as well whose formula is based on the How can I get random integers in a certain range? entry from the C FAQ live example:

#include <iostream>
#include <cstdlib>

int main()
{
    int V = 10 ;

    for (int i = 0; i < V; ++i)
    {
        for (int k = i+1; k < V; ++k)
        {
            int index1 = rand() / (RAND_MAX / V  + 1) + 1 ;
            int index2 = rand() / (RAND_MAX / V + 1) + 1;
            std::cout << "( " <<  index1 << " , " << index2 << ") " ;
        }
        std::cout << std::endl ;
    }
    std::cout << std::endl ;
}

OTHER TIPS

Well, it's just a matter of using std::uniform_real_distribution (assuming you want an uniform distribution in the range 0-100):

int n; // ... populate from input
std::vector<int> numbers(n);

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<int> dis(0, 100);

for (int i = 0; i < n; ++i)
    numbers.push_back(dis(gen));

for (int i : numbers)
    // output i
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top