Can Random.nextgaussian() sample values from a distribution with different mean and standard deviation?

本秂侑毒 提交于 2019-11-27 17:23:50

问题


This is a combined Java and basic math question. The documentation from Random.nextGaussian() states that it samples from a normal distribution with mean 0 and standard deviation 1. What if I wanted to sample from a normal distribution with a different mean and variance?


回答1:


The short answer is

Random r = new Random();
double mySample = r.nextGaussian()*desiredStandardDeviation+desiredMean;

For example this answer is given here: http://www.javamex.com/tutorials/random_numbers/gaussian_distribution_2.shtml

I didn't really understand why this worked, but after looking into it a bit I think I figured it out. The mean of the sample point is 0, and the standard deviation is 1; that means that the original sample is also its own z-score ( https://en.wikipedia.org/wiki/Standard_score ). To quote from wikipedia "The absolute value of z represents the distance between the raw score and the population mean in units of the standard deviation". The formula is z=(x-mean)/stdev, so with the default values z=x. If we wanted to retain the z score for the sample but change the mean and stdev what would we do?

z*stdev + mean = x' where z=x, and x' represents the sample from the distribution with the desired mean and standard deviation.



来源:https://stackoverflow.com/questions/31754209/can-random-nextgaussian-sample-values-from-a-distribution-with-different-mean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!