这是我从阅读你链接的博客中想出来的。据我所知,这是作者为他的第一张图做的。
CSS
#container {
min-width: 310px;
height: 400px;
margin: 0 auto;
}
HTML
<div id="container"></div>
的Javascript
Box–Muller transform
产生Gaussian Random Numbers
var boxMullerRandom = (function() {
var phase = 0,
RAND_MAX,
array,
random,
x1, x2, w, z;
if (crypto && typeof crypto.getRandomValues === 'function') {
RAND_MAX = Math.pow(2, 32) - 1;
array = new Uint32Array(1);
random = function() {
crypto.getRandomValues(array);
return array[0]/RAND_MAX;
};
} else {
random = Math.random;
}
return function() {
if (!phase) {
do {
x1 = 2.0 * random() - 1.0;
x2 = 2.0 * random() - 1.0;
w = x1 * x1 + x2 * x2;
} while (w >= 1.0);
w = Math.sqrt((-2.0 * Math.log(w))/w);
z = x1 * w;
} else {
z = x2 * w;
}
phase ^= 1;
return z;
}
}());
Random Walk
发电机
function randomWalk(steps, randFunc) {
steps = steps >>> 0 || 100;
if (typeof randFunc !== 'function') {
randFunc = boxMullerRandom;
}
var points = [],
value = 0,
t;
for (t = 0; t < steps; t += 1) {
value += randFunc();
points.push([t, value]);
}
return points;
}
助手功能,以从随机游走得到Y值点
function getYValues(points) {
return points.map(function (point) {
return point[1];
});
}
Helper功能,将产生X曲线的图形
function generatePlots(howMany) {
howMany = howMany >>> 0 || 10;
var plots = [],
index;
for (index = 0; index < howMany; index += 1) {
plots.push({
name: 'plot' + index,
data: getYValues(randomWalk())
});
}
return plots;
}
图的结果,使用jQuery
和highcharts.js
$('#container').highcharts({
title: {
text: 'Random Walk',
x: -20 //center
},
subtitle: {
text: 'Random Walk',
x: -20
},
xAxis: {
type: 'linear'
},
yAxis: {
title: {
text: 'Value'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
valueSuffix: ' units'
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
},
series: generatePlots(10)
});
在jsFiddle
这里的第一个相关的搜索查询我能想到的(athough有可能是其他人):https://www.google.com /#q =平滑+随机+步行+算法 –
[这是你在找什么?](http://jsfiddle.net/Xotic750/3rfT6/)这是最好的,我可以看看链接您发布的博客。这是博客上的第一张图,10个100步的图。 – Xotic750
@ Xotic750圣洁的废话;这工作相当好!你能解释一下它是如何工作的,所以我可以修改它,如果需要的话?例如,我怎样才能让它传播更多,或者我怎样才能限制它的价值? –