how to generate a gaussion distribution using random number generator.

8 views (last 30 days)
i want to generate 50,000 samples according to the gaussian distribution using random number generator where sigma=1 and mean=0 X is a normally distributed random variable N(0,σ2). mean=0 variance=1
I did this
R=randn(50000)
i got an error
Error using randn Out of memory. Type HELP MEMORY for your options.
and if i am using
R=randn(50000,1);
hist(R)
this generated a distribution but the y label was ranging from -4 to 5. I want it from -infinity to +infinity
how to do that?
i even tried other ways
G=sqrt(sigma)*randn(50000)+mean
hist(G);
still i am not getting the gaussian distribution range as -infinity to + infinity.
  1 Comment
Guru
Guru on 4 Jul 2013
As pointed out below, but perhaps the question really is why are you expecting the gaussian distribution, or any distribution on a computer really, would ever give you a range of all real numbers, which is what -infinity to +infinity would represent.
On a computer, the range of numbers you can have is limited by the word size of the bits. -infinity and +infinity are reserved in those words as binary 0 followed by the rest of the bits as 1s, or a binary 1 followed by the rest of the bits 0s. The actual minimum and maximum value a computer can store is nowhere close to -infinity or +infinity (these are impossible limits to reach after all). To plot something that goes between these impossible limits is rather pointless, since no matter what is the range of your data, it will simply appear as a line equivalent to y=0. In other words your computer resolution cannot handle distinguishing between y = 0 and y = -1e20 or y = 1e400 when you view it on the scale of -infinity to +infinity. As a mind test, you can ask yourself the following: How many times does 1e400 divide into +infinity? And yes, the answer is Infinity times...

Sign in to comment.

Answers (2)

Shashank Prasanna
Shashank Prasanna on 4 Jul 2013
How do you expect to visualize -Infinity to +Infinity? The probability from 4 sigma to infinity is practically zero: Don't run this if you don't have the statistics toolbox, I am just trying to show why:
>> 1-cdf('normal',4)
ans =
3.1671e-05
Beyond 4, you will have values values that are small enough to disregard.
If you are simply interested in visual appeal use the axis command to widen your x-axis.

Walter Roberson
Walter Roberson on 4 Jul 2013
With variance 1, then each additional whole number away from 0 represent a standard deviation. Reaching +5 was an event 5 standard deviations from the mean. 5 standard deviations is 99.9999426697% cumulative probability, representing all but 1 / 1744278 of probable events. On average you would have to have generated your 50000 samples about 340 times in order to have an event of 5 standard deviations.
randn() cannot generate -infinity to +infinity; the algorithm used is limited to finite values. I do not know what the exact limits are, but I don't think you are going to see more than about 5 * 2^15 standard deviations.
You can force hist to have a bin at -infinity and +infinity, or you could have hist() return the data and then you could bar() plot it with whatever range of x values you want. But do keep in mind that if you generate an infinitely wide plot and scale that to fit your screen width, then any finite data you generated will be scaled to occupy 0 pixels of the infinity. Because even if you had generated an event with googelplex standard deviations, infinity is much greater than googelplex to the googelplex to the googelplex...

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!