MATLAB: Rand function taking a long time

if statementMATLABrandomrandom number generatorspeed

So I have this code below for demonstration a Poisson Distribution. Using an if loop
if rand < (1/AmountRnd)
is much faster than generating a random array
y=rand(10000);
Does anyone know why this is? I have poster both copies of my full code below. Faster Loop Method:
%Set Amount of random numbers to generate

AmountRnd=10000;
%Set length of distribution
Lpos=30;
%Create X array
X=1:1:Lpos;
%Initialize Poisson array
Poisson = zeros(1,Lpos);
%Loop

for ii = 1:100
%Initialise Count

Count = 1;
%Create loop for as many random number are set
for jj = 1:AmountRnd
%Count amount of times rand is less than given amount
if rand < (1/AmountRnd)
Count = Count + 1;
end
end
%Create Poisson array using values from each count
Poisson(Count) = Poisson(Count) + 1;
end
%Graph Histogram of distribution
stairs(Poisson);
Slower Method:
%Set Amount of random numbers to generate
AmountRnd=10000;
Lpos=30;
X=1:1:Lpos;
Poisson = zeros(1,Lpos);
%Loop
for j=1:100
%Initialise Count
Count = 1;
y=rand(10000);
Poisson(Count) = sum(y(1:10000)<(1/10000)) + Poisson(Count) + 1;
end

Best Answer

rand(10000) is the same as rand(10000,10000), not the same as rand(1,10000)
Related Question