def generate(num_nodes, num_edges, directed=False, weight_range=(<NUM_LIT:1>, <NUM_LIT:1>)):
<EOL>if directed:<EOL><INDENT>random_graph = digraph()<EOL><DEDENT>else:<EOL><INDENT>random_graph = graph()<EOL><DEDENT>nodes = range(num_nodes)<EOL>random_graph.add_nodes(nodes)<EOL>edges = []<EOL>edges_append = edges.append<EOL>for x in nodes:<EOL><INDENT>for y in nodes:<EOL><INDENT>if ((directed and x != y) or (x > y)):<EOL><INDENT>edges_append((x, y))<EOL><DEDENT><DEDENT><DEDENT>shuffle(edges)<EOL>min_wt = min(weight_range)<EOL>max_wt = max(weight_range)<EOL>for i in range(num_edges):<EOL><INDENT>each = edges[i]<EOL>random_graph.add_edge((each[<NUM_LIT:0>], each[<NUM_LIT:1>]), wt = randint(min_wt, max_wt))<EOL><DEDENT>return random_graph<EOL>
Create a random graph.

@type  num_nodes: number
@param num_nodes: Number of nodes.

@type  num_edges: number
@param num_edges: Number of edges.

@type  directed: bool
@param directed: Whether the generated graph should be directed or not.  

@type  weight_range: tuple
@param weight_range: tuple of two integers as lower and upper limits on randomly generated
weights (uniform distribution).