I have a code here that generates random numbers having an average value of 0f 1 and std is 0.5. but how can I change this code so that I can degenerate Gaussian random numbers of any given average and variance?
If x is a random variable from the Gaussian distribution with mean ΞΌ and standard deviation Ο , then Ξ±x+Ξ² will have the value Ξ±ΞΌ+Ξ² and standard deviation |Ξ±|Ο .
In fact, the code you posted already performs this conversion. It starts with a random variable with a mean of 0 and a standard deviation of 1 (obtained from the random_normal function that implements Box-Muller transform ), and then converts it into a random variable with a mean of 1 and a standard deviation of 0.5 (in the rands array) by multiplying and additions:
double random_normal(); rands[i] = 1.0 + 0.5*random_normal();
source share