Generate random number between -1 and 1 with specified mean and variance
Show older comments
Hi,
I need help generating a random number between -1 and 1 with a specified mean and variance.
I'm using c=a + b*randn() with 'a' as the mean and 'b' as the standard deviation but the problem is that since the randn function only generates a mean zero standard deviation 1 random variable then I might end up having c greater or less than 1.
Thanks
Accepted Answer
More Answers (2)
Walter Roberson
on 18 Sep 2011
0 votes
It wouldn't matter. randn() always has an infinite tail, so no matter how you scale and shift to get a given mean and variance, the result will never be confined to any give range.
Perhaps you should instead use a beta distribution with a small modification to make it work over [-1,1]
the cyclist
on 18 Sep 2011
0 votes
It is somewhat unusual (although not impossible) to have an real-world application of random numbers in which you want to specify the distribution so much (i.e. mean and variance and range). Do you mind saying what the application is? Maybe there is a better way.
Walter's suggestion of using a beta distribution is a good one. I suggest you look at the Wikipedia page (<http://en.wikipedia.org/wiki/Beta_distribution>) to see how the mean and variance depend on the alpha and beta parameters. You can use the betastat() command to also help you understand that.
The range of the beta distribution is [0,1], not [-1,1], so you'll need to transform the output. (You can multiply by 2 and subtract 1 to get the range you want.) You'll need to be careful about how that transform affects the mean and variance.
Categories
Find more on Random Number Generation in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!