Im trying to generate a list of random (standard) independent normal variables. For this, I first generate a random list of, say, 100 real numbers in the range [0, 1000], and then make them standard independent random variables. However, this strategy is not working in Mathematica. Is there an alternate way to approach this problem?
1 Answer
I think you got what you needed from the comments, but I'll go ahead and post this as further illumination.
Firstly, it's important to note that there's no such thing as a "standard normal distribution" with a "range" (that's how I interpret the OP: you want a range of variates that exhibit a normal distribution): the normal distribution is of infinite extent. Every normal distribution has every real as a possible value.
So, there's a few ways to handle your query, depending on the desired outcome.
Method 1: Taking your desired minimum and maximum, decide how "rare" those extremes need to be, and generate variates with the appropriate mean and deviation. Here's a quick-n-dirty function to do just that. It takes the minimum, maximum, allowed deviation, and a truncation option:
myRV[min_, max_, sdevs_, n_, truncate_: False] :=
RandomVariate[NormalDistribution[Mean[{min, max}], (max - Mean[{min, max}])/sdevs], n] //
If[truncate, Select[#, (min <= # <= max &)], #] &
Called like so: myRV[0,1000,3,10000]
, it will generate 10,000 variates, centered at 500 (the mean), and with the minimum and maximum at three standard deviations from the mean (somewhat rare).
Decreasing the deviation argument means values below or above your specified range become more likely, increasing it makes them less likely. But - because this is a distribution with infinite extent, given enough variates, you might see values outside of your range.
The same function called like so myRV[0,1000,3,10000,True]
will remove any values outside of your specified range. N.b. - in that case, you will have fewer returned variates than requested - the number argument becomes a maximum number of variates. You could change this (e.g., generate more variates than required, trim to desired number), but that's up to you - many ways to do it, just need to ensure you don't end up with parameters that might never generate enough variates to satisfy the request...
Another way of range-limiting the results is to use TruncatedDistribution
, e.g.:
RandomVariate[TruncatedDistribution[{400, 600}, NormalDistribution[500, 200]], 20]
will generate 20 variates with a mean of 500, deviation of 200, limited to between 400 and 600.
Lastly, one might use CensoredDistribution
:
RandomVariate[CensoredDistribution[{400, 600}, NormalDistribution[500, 200]], 20]
Which will generate 20 variates with a mean of 500, deviation of 200, limited to between 400 and 600 as with TruncatedDistribution
, but any values that would have been below 400 or above 600 shifted to 400 and 600, respectively.
For both CensoredDistribution
and TruncatedDistribution
, Mathematica will always return exactly the requested number of variates.
Lastly, a gentle reminder: None of the last three methods are "normal distributions", per se - they are "normal like"...
RandomVariate[NormalDistribution[], 100]
? $\endgroup$NormalDistribution
, e.g.,RandomVariate[NormalDistribution[1000, 100], 100]
- see the docs. for details. $\endgroup$RandomSeed[0]; RandomReal[NormalDistribution[0, 1], 10]
even though help does not mention this option toRandomReal
. ButRandomVariate
is the common way to do this. (I also do not know whatthen make them standard independent random variables
mean) $\endgroup$