随机数在中位数附近 [英] Random Numbers in a range around a median
问题描述
我有一个中位数和一个标准差,我想要的是在中位数std和中位数+ std之间生成随机数.
I have a median and a standard deviation, what i want is to generate random numbers between the median-std and the median+std.
我知道该怎么做:
import numpy as np
import random as rnd
median=30
std=15
random_nr=rnd.randint(median-std,median+std)
我还找到了 numpy .random.normal 函数,但是它似乎没有满足我的需求. 还有其他方法吗? 如果随机数生成器在镜像中生成与中位数相对应的数字,那就太好了,例如,生成的6个随机数的输出应如下所示:
I also found the numpy.random.normal function but it doesn't seem to do what i need. Is there any other way of doing it? It would be great if the random generator would generate numbers in mirror as to the median, for example, an output for 6 generated random numbers should look like this:
numbers=magicfunction(median,std,6)
[29,31,20,40,25,35]
推荐答案
在这里,如果大小为奇数,则只会生成对(根据您的要求),然后在数组末尾生成单个数字. /p>
Here it is, if the size is odd then it will only generate couples (as your request) and then a single number at the end of the array.
import numpy as np
import random as rnd
median=30
std=15
def generatearray(median,std,size):
output=[0]*size
for index in range(0,size/2):
random_nr=rnd.randint(-std,std)
output[2*index]=median+random_nr
output[2*index+1]=median-random_nr
if(size % 2 != 0):
output[size-1]=rnd.randint(median-std,median+std)
return output
print generatearray(median,std,6)
这篇关于随机数在中位数附近的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!