我正在尝试使用 numpy 的 random.rand 生成 0 或 1。
np.random.rand() 产生一个介于 0 和 1 之间的随机 float ,而不仅仅是 0 或 1。
谢谢。
请您参考如下方法:
您可以使用 np.random.choice使用 [0,1] 列表,或使用 np.random.radint范围为 0,2
In [1]: import numpy as np
In [2]: np.random.choice([0,1])
Out[2]: 0
In [5]: np.random.choice([0,1])
Out[5]: 1
In [8]: np.random.randint(2)
Out[8]: 0
In [9]: np.random.randint(2)
Out[9]: 1
您还可以使用 random等价于这些功能的模块
