def random_ports(port, n):
    """Generate a list of n random ports near the given port.

    The first 5 ports will be sequential, and the remaining n-5 will be
    randomly selected in the range [port-2*n, port+2*n].
    (copied from IPython notebookapp.py)
    """
    for i in range(min(5, n)):
        yield port + i

    for i in range(n-5):
        yield max(1, port + random.randint(-2*n, 2*n))
dataset/ETHPy150Open mgymrek/pybamview/pybamview/utils.py/random_ports/original