python學習 random模組

2021-09-25 17:05:22 字數 1441 閱讀 8803

random模組

隨機模組

random 的方法:

---

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

- 注:如果你對python感興趣,我這有個學習python基地,裡面有很多學習資料,感興趣的+q群:688244617--

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

-print

(random.random())

# [0,1] 的浮點數

print

(random.randint(1,

3))print

(random.randrange(1,

3))print

(random.choice([11

,22,33

]))print

(random.sample([11

,22,33

,44,55

],2)

)print

(random.uniform(1,

4))item=[1

,2,3

,4,5

,6]random.shuffle(item)

# 打亂順序

print

(item)

執行結果:

隨機的字母加數字組合

import random

defa_code()

: ret =

''for i in

range(5

):num=random.randint(0,

9)# 隨機數字

alf=

chr(random.randint(65,

122)

)#隨機字母

res=

str(random.choice(

[num,alf]))

ret += res

return ret

print

(a_code(

))

gsn13

process finished with exit code 0

python中numpy的random模組

numpy.random 1.rand d0,d1,dn 產生 0,1 的浮點隨機數,括號裡面的引數可以指定產生陣列的形狀 例如 np.random.rand 3,2 則產生 3 2的陣列,裡面的數是0 1的浮點隨機數 2.randn d0,d1,dn 產生標準正太分布隨機數,引數含義與rand相同...

python中生成隨機整數(random模組)

原文 1 從乙個序列中隨機選取乙個元素返回 random.choice sep 2 用於將乙個列表中的元素打亂 random.shuffle sep 3 在sep列表中隨機選取k個元素返回 random.sample seq,k 4 隨機返回a到b之間的乙個整數 random.randint a,b...

python學習 random模組

random模組常用函式 1 random.random 表示從0 1之間的隨機小數 import random a random.random print a 得到結果 0.1257634562 2 random.randint a,b 表示a x b的乙個整數 import random a r...