How to generate random numbers in a Gaussian distribution?

Given A = 1 and B = 5, and knowing that the mean is 2.5 and the standard deviation is 1, I want to generate 1000 random points between A and B using a normal (Gaussian) distribution. I am unsure how to do this in MATLAB efficienctly.

6 Kommentare

They are not normally distributed if they are bounded as you want. You COULD use a truncated normal distribution however.
help truncate
Ali Almakhmari
Ali Almakhmari am 7 Feb. 2023
Bearbeitet: Ali Almakhmari am 7 Feb. 2023
I see. And what if they are not bounded (assume there is no A and B), how will I do it?
If they are not bounded, you can use randn:
Is this right? I was trying to follow the second example but they used some function called "chol" which I ignored cause I didn't understand..so I am not really sure.
mean = 2.5;
sigma = 1;
values = repmat(mean,1000,1) + randn(1000,1)*sigma
values = mean + randn(1000,1) * sigma;
The repmat() is valid, but slower than needed for this purpose.
I see. Thank you all so much!!

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Swaraj
Swaraj am 8 Feb. 2023
You can use the randn function in MATLAB.
You can follow the following steps:
  1. Generate Random Numbers from a standard normal distribution.
  2. Scale and shift them to match the desired Mean and Standard Deviation.
Please go through the code below:
A = 1;
B = 5;
std = 1;
mean = 2.5;
step1Result = mean + std * randn(1000,1);
step2Result = min(max(step1Result,A),B);

Weitere Antworten (0)

Kategorien

Mehr zu Random Number Generation finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by