想请问一下,这里如何实现self.t = s每次都取随机数,而不是随机后的固定值呢?
from random import choice
import numpy as np
RandomNumber = np.random.choice([0.9, 1, 1.1, 1.2, 1.3, 1.4, 1.5], size=100, replace=True,
p=[0.05, 0.05, 0.15, 0.3, 0.2, 0.1, 0.15])
s = choice(RandomNumber)
class V:
def print_value(self):
self.t = s
w = self.t
for i in range(10):
g = i + w
if g < 100:
print(g)
a = V()
a.print_value()
运行结果
0.9
1.9
2.9
3.9
4.9
5.9
6.9
7.9
8.9
9.9
想要生成比如(类似,但比例遵循p)
1.3 #(0+1.3)
1.9 #(1+0.9
3.2 #(2+1.2)
4.1 #(3+1.1)
4.9 #(4+0.9)
6.5 #(5+1.5)
7.4 #(6+1.4)