what i the difference between rand and randn

95 views (last 30 days)
I would like to know the difference between rand(2,6) and randn(2,6)

Accepted Answer

KSSV
KSSV on 8 Nov 2017
Edited: KSSV on 8 Nov 2017
rand - gives uniformly distributed random numbers
randn - gives Normally distributed random numbers
N = 100000 ;
figure
hist(rand(N,1),100)
title('rand:Uniform distribution')
figure
hist(randn(N,1),100)
title('randn:Normal distribution')
  2 Comments
Steven Lord
Steven Lord on 6 Jun 2022
Using histogram instead of hist (since that is discouraged) you can easily see a difference in the distributions.
N = 1e6;
figure
h1 = histogram(rand(1, N));
ax1 = ancestor(h1, 'axes'); % Use for adjusting limits later
title("rand (uniform distribution)")
figure
h2 = histogram(randn(1, N));
ax2 = ancestor(h2, 'axes'); % Also for adjusting limits
title("randn (normal distribution)")
% Set limits on both axes to the same value, showing both distributions
x1 = ax1.XLim;
x2 = ax2.XLim;
L = [min(x1(1), x2(1)) max(x1(2), x2(2))];
xlim(ax1, L)
xticks(ax1, floor(L(1)):ceil(L(2)))
ax1.XGrid = 'on';
xlim(ax2, L)
xticks(ax2, floor(L(1)):ceil(L(2)))
ax2.XGrid = 'on';

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!