1

如何在 python 2.7 中使用拉丁超立方体采样技术从正态分布中生成 10 个随机数?随机数的范围应该是 5 到 14。

我试过以下

import random
from random import randint
iter = 10
segSize = 1 / iter
for i in range(iter):
segMin = i * segSize
point = segMin+ (random.normalvariate(7.5,1)*segSize)
pointValue = (point * (14 - 5)) + 4
print point
print pointValue

谢谢

4

1 回答 1

1

尝试这个:

def rand:
 import random
 from random import randint
 iter = 10
 segSize = 1/float(iter)
 for i in range(iter):
         segMin = float(i) * segSize
         point = segMin + (random.normalvariate(7.5,1) * segSize)
         pointValue = (point * (14 - 5)) + 4
         print point
         print pointValue

您的问题似乎是整数乘法等,Python 在您的除法中将其截断为零。

当我运行它时,我得到:

0.686848045493
10.1816324094
0.871425699273
11.8428312935
1.08794202088
13.7914781879
1.08502172623
13.7651955361
1.24462345735
15.2016111161
1.10687801576
13.9619021418
1.1394488663
14.2550397967
1.37407532844
16.3666779559
1.54666717385
17.9200045647
1.6465869841
18.8192828569
于 2012-10-14T18:10:27.980 回答