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?


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?).


closed as off-topic by JimB, corey979, m_goldberg, JungHwan Min, Mr.Wizard Dec 24 '16 at 5:52

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – JimB, corey979, m_goldberg, JungHwan Min, Mr.Wizard
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ A related CV thread. $\endgroup$ – J. M. will be back soon Dec 23 '16 at 20:55
  • $\begingroup$ Realted mathematica.stackexchange.com/q/99436/8822 $\endgroup$ – mattiav27 Dec 23 '16 at 20:57
  • 1
    $\begingroup$ 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.) $\endgroup$ – JimB Dec 23 '16 at 21:07
  • 1
    $\begingroup$ 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. $\endgroup$ – JimB Dec 23 '16 at 21:13
  • 1
    $\begingroup$ @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. $\endgroup$ – Jason B. Dec 23 '16 at 21:13

You need to feed a NormalDistribution to RandomVariate:

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

enter image description here

  • 1
    $\begingroup$ I think the OP wants the sample to end up with a mean of 50 and standard deviation of 10. $\endgroup$ – JimB Dec 23 '16 at 21:09
  • $\begingroup$ 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. $\endgroup$ – Kagaratsch Dec 23 '16 at 21:13
  • 5
    $\begingroup$ @JasonB Yep, an infinite sample would work. Please report back when you've got that done. :-} $\endgroup$ – JimB Dec 23 '16 at 21:41
  • $\begingroup$ 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. $\endgroup$ – wolfies Dec 24 '16 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};


(* 50 *)
(* 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]]

enter image description here


Not the answer you're looking for? Browse other questions tagged or ask your own question.