简体   繁体   中英

Random Numbers with Gaussian and Uniform Distributions in matlab

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?

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 .

均匀分布

正态分布

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

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.

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) .

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:

直方图

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

Following raj's answer: by using the Box-Muller Transform you can generate independent standard Normal/Gaussian random numbers:

N = 1e6; z = sqrt(-2*log(rand(N, 1))) .* cos(2*pi * rand(N, 1)); figure; hist(z, 100)
N = 1e6; z = sqrt(-2*log(rand(N, 1))) .* sin(2*pi * rand(N, 1)); figure; hist(z, 100)

If you want to apply the Inverse Transformation Method , you can use the Inverse Complementary Error Function (erfcinv):

N = 1e6; z = -sqrt(2) * erfcinv(2 * rand(1e6, 1)); figure; hist(z, 100)

But I hope randn works better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM