def negative_sample(g, num_samples):
    """Random sample negative edges from graph, excluding self-loops,
    the result samples might be less than num_samples
    """
    num_nodes = g.num_nodes()
    redundancy = _calc_redundancy(num_samples, g.num_edges(), num_nodes**2)
    sample_size = int(num_samples * (1 + redundancy))
    edges = np.random.randint(0, num_nodes, size=(2, sample_size))
    edges = np.unique(edges, axis=1)
    # remove self loop
    mask_self_loop = edges[0] == edges[1]
    # remove existing edges
    has_edges = F.asnumpy(g.has_edges_between(edges[0], edges[1]))
    mask = ~(np.logical_or(mask_self_loop, has_edges))
    edges = edges[:, mask]
    if edges.shape[1] >= num_samples:
        edges = edges[:, :num_samples]
    return edges
Random sample negative edges from graph, excluding self-loops,
    the result samples might be less than num_samples
    
python/dgl/data/adapter.py