0

How to get a random number for given mean and standard error in C++? Are there any built-in functions that can be used straightway?

I am aware of some examples, which using mean and standard deviation to get a random number. However, I do not know the number of replications and only have the mean and standard error, so I would not be able to calculate the standard deviation. Any thoughts?

I appreciate your time and help. Thanks. :-)

alcedine
  • 909
  • 5
  • 18
Ming
  • 215
  • 2
  • 10
  • Do you mean that you want to randomly sample a normal distribution? – alcedine Apr 20 '16 at 08:03
  • 1
    [](http://en.cppreference.com/w/cpp/numeric/random) has several distributions, normal distribution seems the one you want. – Jarod42 Apr 20 '16 at 08:07
  • Hi alcedine, Thanks for your reply. Yes, I want to randomly sample a normal distribution. – Ming Apr 20 '16 at 08:09

2 Answers2

1

There's std::normal_distribution in <random> in C++11, as well as other distributions. You'll need to separately create a generator object and a distribution object; the cppreference.com page I linked has a good example of usage.

Prior to C++11, nothing like that exists in the standard library, though you might want to use Boost.

alcedine
  • 909
  • 5
  • 18
  • Thanks, alcedine! Currently, I would not be able to get standard deviation. I only have data like 20.2 (mean) ± 1.2 (standard error). I would like to have a random number within this range. :-) – Ming Apr 20 '16 at 08:19
  • You would then initialize the distribution object accordingly: `std::normal_distribution<> d(20.2, 1.2);`. – alcedine Apr 20 '16 at 08:22
  • According to the web page, std::normal_distribution<> d(x,y); x is the mean. y is the standard deviation, and not the standard error. Am I correct? Thanks. – Ming Apr 20 '16 at 08:31
  • I'm not sure what the difference is meant to be. – alcedine Apr 20 '16 at 08:39
  • The standard error of the mean (SE of the mean) estimates the variability between sample means that you would obtain if you took multiple samples from the same population. The standard error of the mean estimates the variability between samples whereas the standard deviation measures the variability within a single sample (http://support.minitab.com/en-us/minitab/17/topic-library/basic-statistics-and-graphs/hypothesis-tests/tests-of-means/what-is-the-standard-error-of-the-mean/). – Ming Apr 20 '16 at 08:43
  • That webpage is for the "standard error *of the mean*", while earlier you just said "standard error", which makes me think maybe it's just that your task is using the term "standard error" to refer to the standard deviation (which I initially assumed you were doing). – alcedine Apr 20 '16 at 08:50
  • Sorry for confusing you. I actually mean the "standard error of the mean". – Ming Apr 20 '16 at 08:53
  • Ah, I misunderstood, as this parameter wasn't familiar to me. Well, in that case, you can sample the distribution of the means, but to sample the distribution of the actual population's values you'd need to know the standard deviation, which you can't get unless you know the sample size, unfortunately. – alcedine Apr 20 '16 at 08:59
  • standard error of the mean = standard deviation/√n (n is the size, number of observations, of the sample) – Ming Apr 20 '16 at 09:03
  • Thanks for your help, alcedine! :-) – Ming Apr 20 '16 at 09:03
0

A general answer would be to get a uniform random number on a unit [0,1] and then apply a transformation to map that value to the distribution you need. For a normal distribution, check this post out. Converting a Uniform Distribution to a Normal Distribution

Community
  • 1
  • 1
Alex Moreno
  • 151
  • 1
  • 8