# Matlab--Problem with randn generation

• Nov 15th 2008, 09:10 AM
Kim Nu
Matlab--Problem with randn generation
I wrote a function and a top level script that queries and will return n random numbers that are normally distributed with mean m and standard deviation s.

Here is my function and top level script:

Function:

function[gaussian]=my_gaussian(m,s,n);

gaussian=randn(1,n.*s)+m

Top Level Script:

m=input('Enter the mean of your gaussian random numbers:');
s=input('Enter the standard deviation of your gaussian random numbers:');
n=input('Enter the number of gaussian random numbers:');

[gaussian]=my_gaussian(m,s,n);

Yet when I test the top level script with this:
m=5
s=2
n=500
this program then spits out 1000 numbers instead of 500. Why?

Thanks,

Kim
• Nov 15th 2008, 12:34 PM
Laurent
Quote:

Originally Posted by Kim Nu
I wrote a function and a top level script that queries and will return n random numbers that are normally distributed with mean m and standard deviation s.

Here is my function and top level script:

Function:

function[gaussian]=my_gaussian(m,s,n);

gaussian=randn(1,n.*s)+m

The problem is on this last line: you're asking for a 1-column and $\displaystyle n\times s$-lines matrix of random numbers.
Whereas what you needed is a 1-column and $\displaystyle n$-lines matrix of random numbers, multiplied by $\displaystyle s$. That is to say:
Code:

gaussian=s*randn(1,n)+m
• Nov 16th 2008, 06:07 AM
Kim Nu
Thanks, I thought it was something minor like this. Thanks for all your help.

Kim