Random walks: Difference between revisions
From JSXGraph Wiki
No edit summary |
|||
(One intermediate revision by one other user not shown) | |||
Line 36: | Line 36: | ||
<jsxgraph width="600" height="600"> | <jsxgraph width="600" height="600"> | ||
var brd = JXG.JSXGraph.initBoard('jxgbox', { | var brd = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-100, 100, 100, -100]}); | ||
var t = brd. | var t = brd.create('turtle'); | ||
function run() { | function run() { | ||
Line 44: | Line 44: | ||
t.hideTurtle(); | t.hideTurtle(); | ||
brd.suspendUpdate(); | brd.suspendUpdate(); | ||
var nr = | var nr = document.getElementById('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())); | t.setPenColor(JXG.hsv2rgb(Math.round(Math.random()*255),Math.random(),Math.random())); | ||
Line 56: | Line 56: | ||
t.home(); | t.home(); | ||
} | } | ||
document.getElementById('output').value = (sumdist/nr).toFixed(3); | |||
brd.unsuspendUpdate(); | brd.unsuspendUpdate(); | ||
} | } | ||
Line 65: | Line 65: | ||
===Source code=== | ===Source code=== | ||
<source lang=" | <source lang="javascript"> | ||
var brd = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-100, 100, 100, -100]}); | |||
var brd = JXG.JSXGraph.initBoard('jxgbox', { | var t = brd.create('turtle'); | ||
var t = brd. | |||
function run() { | function run() { | ||
Line 74: | Line 73: | ||
var stepSize = 5; | var stepSize = 5; | ||
brd.suspendUpdate(); | brd.suspendUpdate(); | ||
var nr = | var nr = document.getElementById('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())); | t.setPenColor(JXG.hsv2rgb(Math.round(Math.random()*255),Math.random(),Math.random())); | ||
Line 86: | Line 85: | ||
t.home(); | t.home(); | ||
} | } | ||
document.getElementById('output').value = (sumdist/nr).toFixed(3); | |||
brd.unsuspendUpdate(); | brd.unsuspendUpdate(); | ||
} | } | ||
Line 92: | Line 91: | ||
t.cs(); | t.cs(); | ||
} | } | ||
</source> | </source> | ||
Latest revision as of 12:19, 8 June 2011
Fixed values in this simulation are:
- stepsize [math]\displaystyle{ {}=5 }[/math] and
- Number of steps per walk [math]\displaystyle{ {}= 100 }[/math].
Therefore, the expected squared distance from the starting point will be equal to
- [math]\displaystyle{ 100\cdot 5^2=2500 }[/math].
Source code
var brd = JXG.JSXGraph.initBoard('jxgbox', {boundingbox: [-100, 100, 100, -100]});
var t = brd.create('turtle');
function run() {
var i,j,dist,sumdist=0.0;
var stepSize = 5;
brd.suspendUpdate();
var nr = document.getElementById('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();
}
document.getElementById('output').value = (sumdist/nr).toFixed(3);
brd.unsuspendUpdate();
}
function clearturtle() {
t.cs();
}