def generate_values(spec, num, inputs=None):
try:
# fixed value
fixed = float(spec)
values = [fixed] * num
except ValueError:
parts = spec.split(':')
type = parts[0]
params = parts[1:]
# uniform distribution: u:min:max
if type == "u":
min = float(params[0])
max = float(params[1])
values = [random.uniform(min, max) for _ in xrange(0, num)]
# normal distribution: n:mean:std_dev
elif type == "n":
mean = float(params[0])
std_dev = float(params[1])
values = [random.normalvariate(mean, std_dev) for _ in xrange(0, num)]
# scaled values: x:factor
elif type == "x":
factor = float(params[0])
if inputs is not None:
values = [inputs[i] * factor for i in xrange(0, num)]
else:
print("Inputs are not specified")
sys.exit(-1)
else:
print("Unknown distribution")
sys.exit(-1)
return values
评论列表
文章目录