## This function
## provides us with very simple
## random walk generator compared
## to the function rand_disc_loop
## which is time-consuming, in operation,
## long, and unuseful for other than the
## coin toss simulatons
function xy=rand_disc(N); ## RW generator.
r=floor(rand(N,1)*4); ## Random coulumn vector of N
## integer elements
## multiplied by 4 to widen the
## interval from [0,1] to [0,3].
x=y=zeros(size(r)); ## The coulumn vectors
## of N elements with all elements zero.
x(find(r==0)) = 1; ## The elements of x,
## the vector function
## which takes in the row #
## of the zero elements of
## the vector r as an argument,
## is assigned to 1.
x(find(r==1)) =-1; ## The elements of x,
## the vector function
## which takes in the row # of the
## elements of 1 of the vector r
## as an argument,
## is assigned to -1.
y(find(r==2)) = 1; ## The elements of y,
## the vector function
## which takes in the row # of the
## elements of 2 of the vector r
## as an argument,
## is assigned to 1.
y(find(r==3)) =-1; ## The elements of y,
## the vector function
## which takes in the row # of the
## elements of 3 of the vector r
## as an argument,
## is assigned to -1.
xy=[x y]; ## The resulting x by y
## matrix of a random walk
## with elements 1 and -1.
endfunction
Comments
Post a Comment