我想生成数字(随机),使得数字遵循给定均值和方差的正态分布。我怎样才能做到这一点?
如果你能在Java的上下文中给出它会更好。 人们可能会在这些答案中寻求帮助:但它们并不准确。 Generate random numbers following a normal distribution in C/C++
答案 0 :(得分:0)
无耻地用Google搜索并取自:http://www.javapractices.com/topic/TopicAction.do?Id=62
魔法'发生在Random.nextGaussian()
import java.util.Random;
/**
Generate pseudo-random floating point values, with an
approximately Gaussian (normal) distribution.
Many physical measurements have an approximately Gaussian
distribution; this provides a way of simulating such values.
*/
public final class RandomGaussian {
public static void main(String... aArgs){
RandomGaussian gaussian = new RandomGaussian();
double MEAN = 100.0f;
double VARIANCE = 5.0f;
for (int idx = 1; idx <= 10; ++idx){
log("Generated : " + gaussian.getGaussian(MEAN, VARIANCE));
}
}
private Random fRandom = new Random();
private double getGaussian(double aMean, double aVariance){
return aMean + fRandom.nextGaussian() * aVariance;
}
private static void log(Object aMsg){
System.out.println(String.valueOf(aMsg));
}
}