Random Numbers with Gaussian and Uniform Distributions in matlab

37,894

Solution 1

Use rand(dimensions) for a Uniform Distribution between 0 and 1.

Use randn(dimensions) * sqrt(sigma) + mu for a Gaussian Distribution with a mean of mu and standard deviation of sigma.

Uniform Distribution

Normal Distribution

Solution 2

randn is the function to generate Gaussian distributed variables (randi and rand produce uniformly distributed ones).

Solution 3

You can generate any distribution from rand().

For example , lets say you want to generate 100000 samples for rayleigh dist.The way to do this is that you invert the cdf of that particular function.The basic idea is that since the cdf has to be between 0 and 1 , we can find the value of the random variable by inputting the value of cdf b/w 0 and 1. So for rayleigh, it would be

for i = 1:100000
  data(i) = (2*sigma^2 *(-(log(1 - rand(1,1)))))^.5;
end

You can do something similar for gaussian distribution.

Solution 4

Congrulations, you already generating pseudo-random numbers with a gaussian distribution. Normal distribution is a synonym for it.

The only other possible interpretation I can get from your question is that you want something that has mean != 0 and/or variance != 1. To do that, simply perform mean + sqrt(var) * randn(X).

Solution 5

It is true you can generate just about anything from rand but that it isn't always convenient, especially for some complicated distributions.

MATLAB has introduced Probability Distribution Objects which make this a lot easier and allow you to seamlessly access mean, var, truncate, pdf, cdf, icdf (inverse transform), median, and other functions.

You can fit a distribution to data. In this case, we use makedist to define the probability distribution object. Then we can generate using random.

% Parameters
mu = 10; 
sigma = 3;
a = 5; b = 15;
N = 5000;

% Older Approaches Still Work
rng(1775)
Z = randn(N,1);    % Standard Normal  Z~N(0,1)
X = mu + Z*sigma;  % X ~ Normal(mu,sigma)

U = rand(N,1);     % U ~ Uniform(0,1)
V = a + (b-a)*U;   % V ~ Uniform(a,b)

% New Approaches Are Convenient
rng(1775)
pdX = makedist('Normal',mu,sigma);
X2 = random(pdX,N,1);

pdV = makedist('Uniform',a,b);
V2 = random(pdV,N,1);

A reproducible example:

Histograms

Support = (0:0.01:20)';
figure 
s(1) = subplot(2,2,1)
h(1) = histogram(X,'Normalization','pdf')
xlabel('Normal')
s(2) = subplot(2,2,2)
h(2) = histogram(V,'Normalization','pdf')
xlabel('Uniform')
s(3) = subplot(2,2,3), hold on, box on
h(3) = histogram(X2,'Normalization','pdf')
plot(Support,pdf(pdX,Support),'r-','LineWidth',1.2)
xlabel('Normal (new)')
s(4) = subplot(2,2,4), hold on, box on
h(4) = histogram(V2,'Normalization','pdf')
plot(Support,pdf(pdV,Support),'r-','LineWidth',1.2)
xlabel('Uniform (new)')
xlim(s,[0 20])  

References:
Uniform Distribution
Normal (Gaussian) Distribution

Share:
37,894
Yuseferi
Author by

Yuseferi

Find more about my on About Me

Updated on July 05, 2022

Comments

  • Yuseferi
    Yuseferi almost 2 years

    I want generate a number in Gaussian and Uniform distributions in matlab. I know this function randi and rand() but all of them are in normal (Gaussian) distribution. How can a generate a random number in uniform distribution?