Gaussian distributed random numbers

I need to generate a stationary random numbers with gaussian distribution of zero mean and a variance of unity with max value one.

1 comentario

John D'Errico
John D'Errico el 11 de Jul. de 2014
As all the people have pointed out, there are questions that you must answer before you really get a valid response.
Is the mean to be zero and the variance 1 AFTER truncation or before?

Iniciar sesión para comentar.

 Respuesta aceptada

Star Strider
Star Strider el 11 de Jul. de 2014
The core MATLAB function randn will produce normally-distributed random numbers with zero mean and unity standard deviation.
If you want the numbers to be limited to those <=1, this will work:
q = randn(1,10);
q = q(q<=1);

4 comentarios

José-Luis
José-Luis el 11 de Jul. de 2014
This will also change the variance.
John D'Errico
John D'Errico el 11 de Jul. de 2014
The issue is with the question. Is the variance of the sample to be one AFTER truncation?
José-Luis
José-Luis el 11 de Jul. de 2014
Editada: José-Luis el 11 de Jul. de 2014
I didn't think it through. If you do it like this, the mean will also change, since you are only removing elements from the right tail. John's question remains valid though.
For that matter, considering that the Gaussian distribution has infinite support, once truncated, it is no longer Gaussian.
The mean and variance shift can be ‘fixed’ relatively easily though:
q = q/std(q) - mean(q);
It’s still non-Gaussian, but the numbers work.

Iniciar sesión para comentar.

Más respuestas (2)

Ben11
Ben11 el 11 de Jul. de 2014
What if you generate some random numbers (here 100) with normal distribution, mean of 0 and std dev of 1:
R = normrnd(0,1,1,100);
then divide all by the highest value so that the maximum is 1:
R_norm = R./max(R(:));
Check max:
max(R_norm(:))
ans =
1

2 comentarios

José-Luis
José-Luis el 11 de Jul. de 2014
Then the variance is not one anymore.
Ben11
Ben11 el 11 de Jul. de 2014
Oh shoot you're right

Iniciar sesión para comentar.

Chris E.
Chris E. el 11 de Jul. de 2014
Editada: Chris E. el 11 de Jul. de 2014
Well a simple Gaussian distribution code can be as follows:
function main()
xo = 0;
yo = 0;
xsigma = 0.01;
ysigma = 0.01;
particle_amount = 100;
xpoints = Gauss(xo,xsigma,particle_amount)
ypoints = Gauss(yo,ysigma,particle_amount)
%needs column vectors
coordinates_x_y = [xpoints ypoints];
function output = Gauss(xo,sigma,PA)
r = sqrt(-2.0.*(sigma^2).*log(rand(PA,1)));
phi = 2.0.*pi.*rand(PA,1);
output = xo+r.*cos(phi);
This produces as many random Gaussian distribution about the center of (x,y)=(0,0) and a sigma of 0.01 with 100 points of data. You can modify where needed. I hope that helps you out!

3 comentarios

John D'Errico
John D'Errico el 11 de Jul. de 2014
So randn is not sufficient for you? Note that this does not help, as the request was for a truncated Gaussian.
Jon Thornburg
Jon Thornburg el 22 de Jun. de 2020
This thead is a few years old but I was looking over the example, because I need to do something similar. I was trying the above code. Gauss(xo,xsigma,particle_amount) it pops out the error "Undefined function or variable 'Gauss'."
Gauss was not deifed as a variable and searching matlab documentation cannot find "Gauss" by itself as formated in the above script. Any suggestions?
@Jon Thornburg
Gauss seems to be a user defined function. You would have to put
function output = Gauss(xo,sigma,PA)
r = sqrt(-2.0.*(sigma^2).*log(rand(PA,1)));
phi = 2.0.*pi.*rand(PA,1);
output = xo+r.*cos(phi);
into a new script. You should look up how to implement functions in matlab.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 11 de Jul. de 2014

Comentada:

el 13 de Oct. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by