2017-10-05 87 views
2

numpy.random.uniform支持數組參數,但np.random.randint不會:使用numpy的,以產生具有可變最大/最小隨機整數的序列

import numpy as np 
two_random_uniforms = np.random.uniform([0, 1], [3, 4]) 
this_raises_exception = np.random.randint([0, 1], [3, 4]) 

我從代碼的第二行想要的行爲等同於:

result = [] 
for lown, highn in zip([0, 1], [3, 4]): 
    result.append(np.random.randint(lown, highn)) 

有沒有辦法用變量max/min來完成隨機整數生成而不用編寫python循環?對於我的應用程序需要的大型數組,上述解決方法將會無法接受。我可以在cython中編寫循環,但如果可能的話,我寧願使用Numpy。

回答

2

你可以做的一件簡單的事情就是生成浮動物並圍繞它們。

def my_randint(low, high): 
    return np.floor(np.random.uniform(low, high)) 

這有可能這個過程可能調整所產生的分配了一下,做一些整數非常輕微比其他人更容易,但除非你用密碼或其他一些敏感的應用程序時,它可能贏得」沒關係。

相關問題