Difference between revisions of "Random walks"

From JSXGraph Wiki
Jump to navigationJump to search
Line 38: Line 38:
 
var brd = JXG.JSXGraph.initBoard('jxgbox', {originX: 300, originY: 300, unitX: 3, unitY: 3});
 
var brd = JXG.JSXGraph.initBoard('jxgbox', {originX: 300, originY: 300, unitX: 3, unitY: 3});
 
var t = brd.createElement('turtle');
 
var t = brd.createElement('turtle');
 
HSV2RGB = function(H,S,V) {
 
    var R,G,B, f,i,hTemp, p,q,t;
 
    if (S==0) {
 
        if (isNaN(H)) {
 
            R = V;
 
            G = V;
 
            B = V;
 
        } else {
 
            return '#000000';
 
        }
 
    } else {
 
        if (H>=360) {
 
            hTemp = 0.0;
 
        } else {
 
            hTemp = H;
 
        }
 
        hTemp = hTemp / 60;    // h is now IN [0,6)
 
        i = Math.floor(hTemp);        // largest integer <= h
 
        f = hTemp - i;                  // fractional part of h
 
        p = V * (1.0 - S);
 
        q = V * (1.0 - (S * f));
 
        t = V * (1.0 - (S * (1.0 - f)));
 
        switch (i) {
 
            case 0: R = V; G = t;  B = p; break;
 
            case 1: R = q; G = V;  B = p; break;
 
            case 2: R = p; G = V;  B = t; break;
 
            case 3: R = p; G = q;  B = V; break;
 
            case 4: R = t; G = p;  B = V; break;
 
            case 5: R = V; G = p;  B = q; break;
 
        }
 
    }
 
    R = Math.round(R*255).toString(16); R = (R.length==0)?'00':((R.length==1)?'0'+R:R);
 
    G = Math.round(G*255).toString(16); G = (G.length==0)?'00':((G.length==1)?'0'+G:G);
 
    B = Math.round(B*255).toString(16); B = (B.length==0)?'00':((B.length==1)?'0'+B:B);
 
    return '#'+R+G+B;
 
}
 
 
  
 
function run() {
 
function run() {
Line 84: Line 46:
 
   var nr = $('number').value*1;
 
   var nr = $('number').value*1;
 
   for (i=0;i<nr;i++) {
 
   for (i=0;i<nr;i++) {
     t.setPenColor(HSV2RGB(Math.round(Math.random()*255),Math.random(),Math.random()));
+
     t.setPenColor(JXG.hsv2rgb(Math.round(Math.random()*255),Math.random(),Math.random()));
 
     for (j=0;j<100;j++) {
 
     for (j=0;j<100;j++) {
 
         var a = Math.floor(360*Math.random());  
 
         var a = Math.floor(360*Math.random());  
Line 114: Line 76:
 
   var nr = $('number').value*1;
 
   var nr = $('number').value*1;
 
   for (i=0;i<nr;i++) {
 
   for (i=0;i<nr;i++) {
 +
    t.setPenColor(JXG.hsv2rgb(Math.round(Math.random()*255),Math.random(),Math.random()));
 
     for (j=0;j<100;j++) {
 
     for (j=0;j<100;j++) {
 
         var a = Math.floor(360*Math.random());  
 
         var a = Math.floor(360*Math.random());  

Revision as of 18:44, 22 June 2009

Number of random walks:

Fixed values in this simulation are:

  • stepsize [math]{}=5[/math] and
  • Number of steps per walk [math] {}= 100[/math].

Therefore, the expected squared distance from the starting point will be equal to

  • [math]100\cdot 5^2=2500[/math].

Average square of the distance between starting point and endpoint 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++) {
     t.setPenColor(JXG.hsv2rgb(Math.round(Math.random()*255),Math.random(),Math.random()));
     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() {
  t.cs();
}
</jsxgraph>

External links