# Generate a set of n random numbers with mean $\mu$ and standard deviation $\sigma$? [closed]

What is the most convenient way to generate a sample of i.e. n=500; random numbers (integers? floats?) with mean mu=50; and standard deviation sig=10; using Mathematica?

EDIT:

Links in some comments suggest to implement an own function for that. Certainly I can hack together a whacky solution myself. I am hoping that there is a more convenient way to do this with Mathematica though (built in function?).

• A related CV thread. Dec 23, 2016 at 20:55
• Dec 23, 2016 at 20:57
• There probably isn't (and likely shouldn't) be such a built-in function because one usually samples from a known distribution or has a sample from an unknown distribution and attempts to make inferences about the unknown parameters. Having samples with fixed properties (such as the mean and standard deviation) is likely only useful for creating simple textbook examples. (But I could be too shortsighted on that topic.)
– JimB
Dec 23, 2016 at 21:07
• I'd argue for such a purpose that it's better to fix the parameters (and the distribution) from which you're sampling but use the SeedRandom function so that you always get the same sample when you repeat testing code for errors.
– JimB
Dec 23, 2016 at 21:13
• @Kagaratsch - as usual there's a built-in method to do it, but if you are looking for a fun simple way to do this yourself, you can program a Box-Muller transform in a few short lines. Dec 23, 2016 at 21:13

You need to feed a NormalDistribution to RandomVariate:

data = RandomVariate[NormalDistribution[50, 10], 500];
Through[{ListPlot, Histogram, Mean, StandardDeviation}@data]


• I think the OP wants the sample to end up with a mean of 50 and standard deviation of 10.
– JimB
Dec 23, 2016 at 21:09
• This is actually even better (in my particular case) since the deviation from actual parameters simulates different scenarios for each time the function is called. Dec 23, 2016 at 21:13
• @JasonB Yep, an infinite sample would work. Please report back when you've got that done. :-}
– JimB
Dec 23, 2016 at 21:41
• Must concur with @JimBaldwin: (i) as the question reads, the OP is asking for a random sample, such that its mean is 50 and standard deviation is 10, given a sample of size 500. Drawing from $N(50,100)$ does not satisfy these requirements, because the sample mean and standard deviation will not equal those of the population. (ii) In any event, in reply to the opening line You need to feed a NormalDistribution ... you don't need to feed the Normal -- the Normal distribution is just one of an infinite number of generators, and not necessarily the most convenient or appropriate. Dec 24, 2016 at 6:39

If you really need to have your sample with a known mean and standard deviation, here's how you can do it.

Get a list of numbers in any way you want (as long as not all numbers are identical). It can be a random sample or a mixture any lists you have (unless you require a specific distribution).

x = RandomVariate[LogNormalDistribution[1, 2], 500];


Now standardize x (by subtracting the mean of x and dividing by the standard deviation of x) followed by multiplying by the desired σ and adding in the desired μ:

y = (μ + σ Standardize[x]) /. {μ -> 50, σ -> 10};


Now

Mean[y]
(* 50 *)
StandardDeviation[y]
(* 10 *)


It's no fun to have a normal distribution. You want to test your code against weird cases!

So I guess the most "primitive" distribution that has predefined mean and standard deviation would be following:

 rand[mu_, sigma_, n_] := RandomSample@Flatten@ConstantArray[{mu - sigma Sqrt[(n - 1)/n],
mu + sigma Sqrt[(n - 1)/n]}, n/2]


And stealing @JasonB's visualization code:

Through[{ListPlot, Histogram[#, {1}] &, Mean, StandardDeviation}@rand[50, 10, 500]]