I would like to generate multiple Erdos-Renyi graphs with random edge weights. However, my code works quite slow since there are two nested loops. I was wondering if someone can help me with improving my code.
import networkx as nx
import random
#Suppose I generate 1000 different random graphs
for _ in range(1000):
#Let's say I will have 100 nodes and the connection probability is 0.4
G= nx.fast_gnp_random_graph(100,0.4)
#Then, I assign random edge weights.
for (u, v) in G.edges():
G.edges[u,v]['weight'] = random.randint(15,5000)
When I run a similar code block in R using igraph, it is super fast regardless of the size of the network. What are some alternative ways that I can accomplish the same task without facing slow execution time?