Code covered by the BSD License  

Highlights from
Random Walks in MATLAB

image thumbnail

Random Walks in MATLAB

by

 

05 Nov 2008 (Updated )

Files which simulate random walks on a lattice and on a graph

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

RandDir(N)
function out = RandDir(N)

% Generate a random vector from the set {+/- e_1, +/- e_2,..., +/- e_N}
% where e_i is the ith basis vector. N should be an integer.

I = round(ceil(2*N*rand));

if rem(I,2) == 1
    sgn = -1;
else
    sgn = 1;
end
out = zeros(N,1);

out(ceil(I/2)) = sgn*1;

end

Contact us