Inhomogeneous Poisson spike train generation

def gen_spikes(r,dt,rng):
    '''Generate spike train from Poisson rate
 
    Parameters
    ----------
    r:   Array of spike rates
    dt:  Time step
    rng: Random number generator
    '''
    i = 0
    s = 0
    spks = []
    while True:
        s += rng.exponential()
        while s>r[i]*dt:
            s -= r[i]*dt
            i += 1
            if i>=len(r): break
        else:
            spks.append(i*dt+s/r[i])
            continue
        break
    return np.array(spks)