dgl.rand_graph

dgl.rand_graph(num_nodes, num_edges, idtype=torch.int64, device=device(type='cpu'))[source]

Generate a random graph of the given number of nodes/edges and return.

It uniformly chooses num_edges from all possible node pairs and form a graph. The random choice is without replacement, which means there will be no multi-edge in the resulting graph.

To control the randomness, set the random seed via dgl.seed().

Parameters
  • num_nodes (int) – The number of nodes

  • num_edges (int) – The number of edges

  • idtype (int32, int64, optional) – The data type for storing the structure-related graph information such as node and edge IDs. It should be a framework-specific data type object (e.g., torch.int32). By default, DGL uses int64.

  • device (Device context, optional) – The device of the resulting graph. It should be a framework-specific device object (e.g., torch.device). By default, DGL stores the graph on CPU.

Returns

The generated random graph.

Return type

DGLGraph

See also

rand_bipartite()

Examples

>>> import dgl
>>> dgl.rand_graph(100, 10)
Graph(num_nodes=100, num_edges=10,
      ndata_schemes={}
      edata_schemes={})