import random


def rand_dgraph(n, p):
    """ Generate a random directed graph with n nodes and an
    edge probability of p.
    """
    graph = dict()
    for i in range(n):
        graph[i]=[]
        for i2 in range(n):
            if random.random()<=p:
                graph[i].append(i2)
    return graph