2

I want to know if there is a better way to simulate numbers that form normal distribution with its mean equals zero(0) and its standard deviation(sd) equals one(1) other than

nn <- rnorm(n=20, mean=0, sd=1)

without standard normal process such that when I run mean(nn) and sd(nn) function in R it will always give me 0 and 1 respectively

Daniel James
  • 1,381
  • 1
  • 10
  • 28
  • I don't understand your edit. If you are asking for something than the existing answer or the linked duplicate, please clarify what you want (it's not to clear to me in its current form)/why the existing answers don't do what you want. – Ben Bolker Jun 26 '20 at 23:39
  • my edit is that I do not want it to be in the standard normal way like it is done here https://stackoverflow.com/questions/18919091/generate-random-numbers-with-fixed-mean-and-sd – Daniel James Jun 26 '20 at 23:56
  • Why not? How do you want it done? Do you mean you want to avoid the `rnorm()` function? It's possible to do (e.g. by reimplementing the [Box-Muller transformation](https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform) in R), but it's going to be hard to convince anyone to spend time on it if you don't give a reason - a reimplementation will be slower, less robust, and less well tested than using the built-in `rnorm()` ... – Ben Bolker Jun 27 '20 at 00:13
  • If your question is "is there a better way to generate standard normal deviates in R than using `rnorm()` and scaling the results", the answer is 'probably not'. In what way would you like the results to be better? Faster? There is a function described [here](https://rdrr.io/cran/Rfast/man/Rnorm.html) that claims to be faster than `rnorm()` for large numbers of deviates (you'd still have to scale them yourself). – Ben Bolker Jun 27 '20 at 00:17
  • @Ben Bolker do not force me to accept your answer to this question. The two questions are never the same. This is a forum to get help from different opinions and I am confident of brain here. What you do not think to exist could be possible by many people. Please the duplicate tag on this question is not necesary because the two question are never the same. – Daniel James Jun 27 '20 at 01:39
  • I'm willing to vote to reopen this (some others have to do so as well; I can't reopen it myself). Can you please **edit your question** to clarify it further? Just so I'm clear, your question is "how can I generate Normal deviates with a mean of exactly 0 and standard deviation of exactly 1 *without using the built-in `rnorm()` function*?" Right? It would help a lot if you explained why you are interested in doing this. – Ben Bolker Jun 27 '20 at 20:11
  • On the other hand, if your question is "is there a better way to generate ... without using the built-in `rnorm()` function"? (which is really a different question), then you should explain what you mean by "better" ("faster" is the only possible meaning I can think of, but maybe that isn't what you mean) – Ben Bolker Jun 27 '20 at 20:12

1 Answers1

2

You could just normalize it arithmetically:

rnorm2 <- function(n, mean = 0, sd = 1)
{
  x <- rnorm(n, mean, sd)
  x <- x - mean(x)
  x <- x/sd(x)
  x
}

set.seed(123)
x <- rnorm2(20)
x
#>  [1] -0.72183046 -0.38224997  1.45690865 -0.07311396 -0.01268275  1.61765939
#>  [7]  0.32826544 -1.44621692 -0.85175922 -0.60379019  1.11287817  0.22432179
#> [13]  0.26643044 -0.03181062 -0.71706570  1.69152668  0.36623767 -2.16748857
#> [19]  0.57546218 -0.63168204
mean(x)
#> [1] 1.501078e-17
sd(x)
#> [1] 1

A couple of things to note though: this is not better than a normal distribution. You can't take a single random number (since its s.d. can't be normalised to 1), and you will always get the same result for 2 random numbers (only one pair of numbers will sum to 0 with an s.d. of 1). Effectively you have removed a degree of freedom from the sample.

Created on 2020-06-26 by the reprex package (v0.3.0)

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87