Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating a random number based off normal distribution in matlab

I am trying to generate a random number based off of normal distribution traits that I have (mean and standard deviation). I do NOT have the Statistics and Machine Learning toolbox.

I know one way to do it would be to randomly generate a random number r from 0 to 1 and find the value that gives a probability of that random number. I can do this by entering the standard normal function

f= @(y) (1/(1*2.50663))*exp(-((y).^2)/(2*1^2))

and solving for

r=integral(f,-Inf,z)

and then extrapolating from that z-value to the final answer X with the equation

z=(X-mew)/sigma

But as far as I know, there is no matlab command that allows you to solve for x where x is the limit of an integral. Is there a way to do this, or is there a better way to randomly generate this number?

like image 625
curt Avatar asked Dec 02 '16 21:12

curt


People also ask

How do you generate a random number in a normal distribution in MATLAB?

X = randn returns a random scalar drawn from the standard normal distribution. X = randn( n ) returns an n -by- n matrix of normally distributed random numbers.

How do you generate a random number from a distribution?

The inversion method relies on the principle that continuous cumulative distribution functions (cdfs) range uniformly over the open interval (0,1). If u is a uniform random number on (0,1), then x = F - 1 ( u ) generates a random number x from any continuous distribution with the specified cdf F .

How do I generate a list of random numbers in MATLAB?

You can use the randperm function to create a double array of random integer values that have no repeated values. For example, r4 = randperm(15,5);


1 Answers

You can use the built-in randn function which yields random numbers pulled from a standard normal distribution with a zero mean and a standard deviation of 1. To alter this distribution, you can multiply the output of randn by your desired standard deviation and then add your desired mean.

% Define the distribution that you'd like to get
mu = 2.5;
sigma = 2.0;

% You can any size matrix of values
sz = [10000 1];

value = (randn(sz) * sigma) + mu;

%   mean(value)
%       2.4696
%
%   std(value)
%       1.9939

If you just want a single number from the distribution, you can use the no-input version of randn to yield a scalar

value = (randn * sigma) + mu;
like image 74
Suever Avatar answered Oct 04 '22 16:10

Suever