Friday, February 5, 2010

2D Random walk program

Why I want to do this?
http://en.wikipedia.org/wiki/Random_walk

Here's the code I wrote:

% random walk
x=[];
y=[];
x(1)=0;
y(1)=0;
for i=1:100000
J=rand;
if J<0.25
x(i+1)=x(i)+1;
y(i+1)=y(i);
elseif (J>0.25)&(J<0.5)
x(i+1)=x(i)-1;
y(i+1)=y(i);
elseif (J>0.5)&(J<0.75)
x(i+1)=x(i);
y(i+1)=y(i)+1;
else
x(i+1)=x(i);
y(i+1)=y(i)-1;
end
end

plot(x,y)

And here are the figure I got:

9 comments:

  1. It worked great! It also works by adding the z-dimension in a similar fashion.

    Thanks!
    Julian

    ReplyDelete
  2. Yes,..not only a working program but it is well written and logically laid out, which makes a much better program,
    thanks for sharing such high quality work!

    ReplyDelete
  3. Thanks for your comments, Antolin and jccbama.

    ReplyDelete
  4. sir I will need your picture and a statue of yourself will be built next to my programming class! thank you!

    ReplyDelete
  5. Dear Anonymous, I am so flattered...

    ReplyDelete
  6. FYI, you have a slight problem if the random number IS 0.25, 0.5, or 0.75... it'll automatically interpret it as >0.75. I would've added in "<=" to avoid that.

    ReplyDelete
  7. Hello,

    Great information! I’ve been looking for something like this for a while now. Thanks!

    Female fitness program

    ReplyDelete
  8. this program lacks comment. for example what is exactly J? why those conditions! but its great overall very simple

    ReplyDelete
    Replies
    1. I agree with you. I was not aware of the importance of commenting by the time I wrote this code, which is six years ago. But now I do! Thank you very much!

      Delete

Any comments?

my-alpine and docker-compose.yml

 ``` version: '1' services:     man:       build: .       image: my-alpine:latest   ```  Dockerfile: ``` FROM alpine:latest ENV PYTH...