我想定义500个变量。所有这些都应该是从binomal分布生成的随机数。然后我想在HTML页面中显示它们中的每一个。
我的代码如下:
# draw a number from the distribution
def a1s1(self):
return 3 * np.random.binomial(1, 0.3)
# get rid of the decimal point
def a1s1int(self):
return int(self.a1s1())
# define 500 variables
number1 = models.FloatField()
number2 = models.FloatField()
....# this should continue in this way until "number500"
# generate a list of 500 random numbers
def drawnumbers(self):
rnlist = []
numbers = dict()
for i in range(1, 500):
numbers[i] = self.a1s1int()
rnlist.append(numbers)
return numbers
# to get each element from the list
number1 = self.drawnumbers[1]
number2 = self.drawnumbers[2]
这给了我错误:
NameError: name 'self' is not defined
有时我会收到另一个错误" NoneType"不可赎回。
你可以帮忙吗?我是Python新手。非常感谢。答案 0 :(得分:0)
直接构建列表更好:
number=[int(3*np.random.binomial(1,0.3)) for i in range(500)]
例如,number[23]
可以访问它们。