%Script to do simulation of random walk in 2D % %adjust parameters below for probability of left step, %right step, or no step. %by Nasser Abbasi, CSUF, spring 2008, Mathematics 504 clear all; close all; % % C O N F I G U R A T I O N change as needed % make sure the probabilites add to ONE % I am not going to check for this :) % p = 1/2; %probability of RIGHT step q = 1/2; %probability of LEFT step z = 0; %probability of NO step nSteps = 300; %how many steps seed = 10101010; %my seed rand('twister',seed); % % G E N E R A T E R A N D O M S T E P S % Generate random numbers from 1,-1,0 with probability p,q,z % steps = ones(nSteps,1); k = round(q*nSteps); steps(1:k) = -1; steps(k+1:k+round(z*nSteps)) = 0; indx = randperm(nSteps); steps = steps(indx); % % Start the simulation % lastX = 0; lastY = 0; figure(1); hold on; for i=1:nSteps currentY = lastY + steps(i); currentX = lastX + 1; line([lastX currentX currentX],[lastY lastY currentY]); line([0 nSteps],[0 0],'Color','r'); lastX = currentX; lastY = currentY; ylim([round(-0.15*q*nSteps),round(0.15*p*nSteps)]); title(sprintf('R.W. simulation p(+1)=%3.2f, p(-1)=%3.2f, p(0)=%3.2f, current Step=%d'... ,p,q,z,i)); drawnow; pause(0.05); end