import random
import numpy as np
lista = []
listb = []
for _ in range(10):
a = np.random.randint(0, 2)
b = random.randint(0, 2)
lista.append(a)
listb.append(b)
print(lista)
print(listb)
运行结果:
[0, 0, 0, 0, 1, 0, 0, 1, 0, 1]
[0, 1, 0, 1, 2, 0, 2, 1, 0, 0]
可以看出:
np.random.randint 不包含上限
random.randint包含上限
使用情况举例
if random.randint(0, 1):
pass
else:
pass
作为条件判断代码显得简洁