% Random walk (simple) % January 2021 by AG. itests = 5000; rng('shuffle') % use seed as current time fprintf('Simple random walk \n'); fprintf('iTests = %5i \n', itests); %fileID = fopen('random2.txt','w'); % First loop - number of steps for isteps=0:10:200 dav=0.0; % Second loop - number of tests for it=1:itests x=0; y=0; r = randi([0 3],1,isteps); % Thirst loop - walking isteps for is=1:isteps iway= r(is); switch iway case 0 x = x + 1; case 1 x = x - 1; case 2 y = y + 1; case 3 y = y - 1; end end %exnd loop 3 d = sqrt(x*x +y*y); dav = dav + d; end %end loop 2 dav2 = dav/double(itests); %fprintf(fileID,'%4i %10f \n', isteps, dav2); fprintf('%4i %10f \n', isteps, dav2); end % end loop 1