天天看点

【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
           

继续阅读