Difference between revisions of "Random walks"

From JSXGraph Wiki
Jump to navigationJump to search
Line 57: Line 57:
 
<source lang="html4strict">
 
<source lang="html4strict">
 
<jsxgraph width="600" height="600">
 
<jsxgraph width="600" height="600">
 +
var brd = JXG.JSXGraph.initBoard('jxgbox', {originX: 300, originY: 300, unitX: 3, unitY: 3});
 +
var t = brd.createElement('turtle');
 +
 +
function run() {
 +
  var i,j,dist,sumdist=0.0;
 +
  var stepSize = 5;
 +
  brd.suspendUpdate();
 +
  var nr = $('number').value*1;
 +
  for (i=0;i<nr;i++) {
 +
    for (j=0;j<100;j++) {
 +
        var a = Math.floor(360*Math.random());
 +
        t.right(a);
 +
        t.forward(stepSize);
 +
    }
 +
    dist = t.pos[0]*t.pos[0]+t.pos[1]*t.pos[1];
 +
    sumdist += dist;
 +
    t.home();
 +
  }
 +
  $('output').value = (sumdist/nr).toFixed(3);
 +
  brd.unsuspendUpdate();
 +
}
 +
function clearturtle() {
 +
  sumist = 0.0
 +
  t.cs();
 +
}
 
</jsxgraph>
 
</jsxgraph>
 
</source>
 
</source>
  
 
===External links===
 
===External links===
* []
+
* [http://en.wikipedia.org/wiki/Random_walk http://en.wikipedia.org/wiki/Random_walk]
  
 
[[Category:Examples]]
 
[[Category:Examples]]
 
[[Category:Turtle Graphics]]
 
[[Category:Turtle Graphics]]

Revision as of 19:30, 26 May 2009

Number of random walks:

Average square length of the walks:

Source code

<jsxgraph width="600" height="600">
var brd = JXG.JSXGraph.initBoard('jxgbox', {originX: 300, originY: 300, unitX: 3, unitY: 3});
var t = brd.createElement('turtle');

function run() {
  var i,j,dist,sumdist=0.0;
  var stepSize = 5; 
  brd.suspendUpdate();
  var nr = $('number').value*1;
  for (i=0;i<nr;i++) {
     for (j=0;j<100;j++) {
        var a = Math.floor(360*Math.random()); 
        t.right(a); 
        t.forward(stepSize);
     }
     dist = t.pos[0]*t.pos[0]+t.pos[1]*t.pos[1];
     sumdist += dist;
     t.home();
  }
  $('output').value = (sumdist/nr).toFixed(3);
  brd.unsuspendUpdate();
}
function clearturtle() {
  sumist = 0.0
  t.cs();
}
</jsxgraph>

External links