天天看點

【ChatBot開發筆記】随機數種

【ChatBot開發筆記】随機數種

随機數種

計算機内的随機都是僞随機,python就是基于Mersenne Twister。

Mersenne Twister是現存最廣泛測試的随機數發生器之一,但是它是完全确定的。在python中如果直接使用random.random(),那可以擷取到均勻的随機數(随機數種随機),也可以可以通過設定随機數種然後調用random.random(),來得到完全一樣的随機數,是以它并不适合安全用途。

實驗總需要打亂資料來操作,但如果每次都打亂資料後與上一次資料有差異,則實驗結果沒辦法複現,改進的DEBUG也會變得困難,故設定随機種子,使得打亂過程不是完全随機的,而是以指定的方式打亂。

系統的随機數種

import random

random.seed(1)
print('随機數1:',random.random(),"    (seed 1)")
random.seed(1)
print('随機數2:',random.random(),"    (seed 1)")
random.seed(1.1) 
print('随機數3:',random.random(),"    (seed 1.1)")
random.seed(1.1)
print('随機數4:',random.random(),"    (seed 1.1)")
random.seed(3.456)
print('随機數5:',random.random(),"    (seed 3.456)")
random.seed()
print('随機數6:',random.random(),"    (no seed)")
print('随機數7:',random.random(),"    (no seed)")
print('随機數8:',random.random(),"    (no seed)")
print('随機數9:',random.random(),"    (no seed)")
random.seed(1)
print('随機數10:',random.random(),"    (seed1)")

随機數1: 0.13436424411240122     (seed 1)
随機數2: 0.13436424411240122     (seed 1)
随機數3: 0.22415427849366876     (seed 1.1)
随機數4: 0.22415427849366876     (seed 1.1)
随機數5: 0.7812229172936054     (seed 3.456)
随機數6: 0.8903081131998817     (no seed)
随機數7: 0.6682693570457604     (no seed)
随機數8: 0.4221332899465733     (no seed)
随機數9: 0.8824663095554685     (no seed)
随機數10: 0.13436424411240122     (seed1)
           

numpy的随機數種

import numpy as np

np.random.seed(0)
print(np.random.rand(4,3))

print("***********************************")

np.random.seed(0)
print(np.random.rand(4,3))


[[0.5488135  0.71518937 0.60276338]
 [0.54488318 0.4236548  0.64589411]
 [0.43758721 0.891773   0.96366276]
 [0.38344152 0.79172504 0.52889492]]
***********************************
[[0.5488135  0.71518937 0.60276338]
 [0.54488318 0.4236548  0.64589411]
 [0.43758721 0.891773   0.96366276]
 [0.38344152 0.79172504 0.52889492]]
           

随機數工具1

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import random
import string

# 随機整數:
print (random.randint(1,50))

# 随機選取0到100間的偶數:
print (random.randrange(0, 101, 2))

# 随機浮點數:
print (random.random())
print (random.uniform(1, 10))

# 随機字元:
print (random.choice('[email protected]#$%^&*()'))

# 多個字元中生成指定數量的随機字元:
print (random.sample('zyxwvutsrqponmlkjihgfedcba',5))

# 從a-zA-Z0-9生成指定數量的随機字元:
ran_str = ''.join(random.sample(string.ascii_letters + string.digits, 8))
print (ran_str)

# 多個字元中選取指定數量的字元組成新字元串:
print (''.join(random.sample(['z','y','x','w','v','u','t','s','r','q','p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a'], 5)))

# 随機選取字元串:
print (random.choice(['剪刀', '石頭', '布']))

# 打亂排序
items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
print (random.shuffle(items))

42
96
0.8633618894417576
6.686603838697243
w
['e', 't', 'w', 'k', 'a']
rP2lyYKI
cqrxo
布
None
           

随機數工具1

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import random
import string

# 随機整數:
print (random.randint(1,50))

# 随機選取0到100間的偶數:
print (random.randrange(0, 101, 2))

# 随機浮點數:
print (random.random())
print (random.uniform(1, 10))

# 随機字元:
print (random.choice('[email protected]#$%^&*()'))

# 多個字元中生成指定數量的随機字元:
print (random.sample('zyxwvutsrqponmlkjihgfedcba',5))

# 從a-zA-Z0-9生成指定數量的随機字元:
ran_str = ''.join(random.sample(string.ascii_letters + string.digits, 8))
print (ran_str)

# 多個字元中選取指定數量的字元組成新字元串:
print (''.join(random.sample(['z','y','x','w','v','u','t','s','r','q','p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a'], 5)))

# 随機選取字元串:
print (random.choice(['剪刀', '石頭', '布']))

# 打亂排序
items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
print (random.shuffle(items))

42
96
0.8633618894417576
6.686603838697243
w
['e', 't', 'w', 'k', 'a']
rP2lyYKI
cqrxo
布
None
           

随機數工具1

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import random
import string

# 随機整數:
print (random.randint(1,50))

# 随機選取0到100間的偶數:
print (random.randrange(0, 101, 2))

# 随機浮點數:
print (random.random())
print (random.uniform(1, 10))

# 随機字元:
print (random.choice('[email protected]#$%^&*()'))

# 多個字元中生成指定數量的随機字元:
print (random.sample('zyxwvutsrqponmlkjihgfedcba',5))

# 從a-zA-Z0-9生成指定數量的随機字元:
ran_str = ''.join(random.sample(string.ascii_letters + string.digits, 8))
print (ran_str)

# 多個字元中選取指定數量的字元組成新字元串:
print (''.join(random.sample(['z','y','x','w','v','u','t','s','r','q','p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a'], 5)))

# 随機選取字元串:
print (random.choice(['剪刀', '石頭', '布']))

# 打亂排序
items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
print (random.shuffle(items))

42
96
0.8633618894417576
6.686603838697243
w
['e', 't', 'w', 'k', 'a']
rP2lyYKI
cqrxo
布
None
           

随機數工具1

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import random
import string

# 随機整數:
print (random.randint(1,50))

# 随機選取0到100間的偶數:
print (random.randrange(0, 101, 2))

# 随機浮點數:
print (random.random())
print (random.uniform(1, 10))

# 随機字元:
print (random.choice('[email protected]#$%^&*()'))

# 多個字元中生成指定數量的随機字元:
print (random.sample('zyxwvutsrqponmlkjihgfedcba',5))

# 從a-zA-Z0-9生成指定數量的随機字元:
ran_str = ''.join(random.sample(string.ascii_letters + string.digits, 8))
print (ran_str)

# 多個字元中選取指定數量的字元組成新字元串:
print (''.join(random.sample(['z','y','x','w','v','u','t','s','r','q','p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a'], 5)))

# 随機選取字元串:
print (random.choice(['剪刀', '石頭', '布']))

# 打亂排序
items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
print (random.shuffle(items))

42
96
0.8633618894417576
6.686603838697243
w
['e', 't', 'w', 'k', 'a']
rP2lyYKI
cqrxo
布
None
           

随機數工具2

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import random

print( random.randint(1,10) )        # 産生 1 到 10 的一個整數型随機數  
print( random.random() )             # 産生 0 到 1 之間的随機浮點數
print( random.uniform(1.1,5.4) )     # 産生  1.1 到 5.4 之間的随機浮點數,區間可以不是整數
print( random.choice('tomorrow') )   # 從序列中随機選取一個元素
print( random.randrange(1,100,2) )   # 生成從1到100的間隔為2的随機整數

a=[1,3,5,6,7]                # 将序列a中的元素順序打亂
random.shuffle(a)
print(a)

6
0.3719465925363282
5.102804982225576
t
13
[3, 6, 1, 5, 7]
           

本項目的随機數種設定方法,統一為666

def set_random_seed(args):
    """
    設定訓練的随機種子
    """
    # 為CPU設定種子
    torch.manual_seed(args.seed)
    # 為python内置random函數設定種子
    random.seed(args.seed)
    # 為numpy的random函數設定種子
    np.random.seed(args.seed)

    # 是否調用cuDNN 的 auto-tuner
    if args.cuda:
        # 拒絕計算随機性
        torch.backends.cudnn.deterministic = True
        torch.backends.cudnn.benchmark = False
           

繼續閱讀