在看教學文章時,常常搞不清楚random.random , random.rand , random.randn 一堆看似類似但卻又不太一樣的函數。為了學的踏實點,就去了解了下:
import random # random just return one object import numpy as np # use numpy.random will return one or an array object a = random.random() # return one object aa = random.random( 10 ) # fail, because it only return one object b = random.randint( 10 , 20 ) # return an int object between 10~20 c = random.uniform( 10 , 20 ) #return an object between 10~20 d = random.randrange( 10 , 20 , 3 ) #return an int object in range 10~20 (such 10, 13,16,19) e = random.choice(( 'pass' , 'fail' , 'null' )) # random choice one object A = np.random.random() # same as random.random() AA = np.random.random( 10 ) # 更新:不建議這樣用,因為np.random.random(10,4)的矩陣會回報錯 # can return an array G = np.random.rand( 10 ) # 更新:np.random.rand(10,4)的矩陣仍可以過 所以用矩陣的話寫rand就好 # same as np.random.random(10) F = np.random.randn() # return an normal distribution or Gaussian distribution object in random FF = np.random.randn( 10 ) # return an normal distribution or Gaussian distribution array in random B = np.random.randint( 10 , 20 ) # same as random.randint(10,20) CC = np.random.uniform( 10 , 20 ) # same as random.uniform(10,20) DD = np.random.randrange( 10 , 20 , 3 ) # fail, becauseno attribute randrange in np.random |
總的來說,如果要出一個隨機數,就用random的就好。
如果需要用到array或矩陣的再引用np.random 免得搞混
這封郵件來自 Evernote。Evernote 是您專屬的工作空間,免費下載 Evernote |
0 意見:
張貼留言