Random walks on a grid, Javascript

Some quick Javascript which plots random walks from a point…

View it here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<html>
<head>
 
<script type="text/javascript">
 
  var ittr=8;
  var delta=-0.005;
 
  function draw_frame() {
    var canvas = document.getElementById("m_canvas");
    if(canvas.getContext) {
      var ctx = canvas.getContext("2d");
      ctx.lineWidth  = 0;
      ctx.fillStyle = "rgb(0,0,0)";
 
 
      var x=250;
      var y=250;
      for(var n=0;n<200;n++) {
        var r=Math.floor(Math.random()*4);
        if(r==0) { x+=1; y+=0; }
        if(r==1) { x+=0; y+=1; }
        if(r==2) { x-=1; y+=0; }
        if(r==3) { x-=0; y-=1; }
        ctx.fillRect(x*1,y*1,1,1);
      }
 
      ctx.fillStyle = "rgb(0,200,200)";
      ctx.fillRect(250,250,3,3);
    }
  }
 
</script>
 
</head>
 
<body  onload="setInterval(draw_frame,10);" >
<canvas id="m_canvas" width="500" height="500">You need a Javascipt</canvas>
 
</body>
 
</html>

Leave a Reply