x_n = (421 * x_{n-1} + 3) mod 1e+16
?????ɡ?
<html><head><title>c4sey Randoms</title></head><body><script type="application/javascript"> /* ========== cross env ========== */ if(!Array.prototype.clone) Array.prototype.clone = function(){ var a = new Array(); for(var i=0; i<this.length; i++){ if(this[i]._className != "Array") a[a.length] = this[i]; else a[a.length] = this[i].clone(); } return a; }; if(!Array.prototype.forEach) Array.prototype.forEach = function(f, o){ if(o == null) o = undefined; for(var i=0; i<this.length; i++){ if(!this[i]) continue; f.call(o, this[i], i, this); } return this; }; if(!Array.prototype.map) Array.prototype.map = function(f, o){ if(o == null) o = undefined; var a = new Array(); for(var i=0; i<this.length; i++){ if(!this[i]) continue; a[a.length] = f.call(o, this[i], i, this); } return a; }; if(!Array.prototype.reduce) Array.prototype.reduce = function(f, v){ var i = 0; if(v == null){v = this[0]; i = 1;} for(i; i<this.length; i++){ if(!this[i]) continue; v = f.call(null, v, this[i], i, this); } return v; }; if(!Array.prototype.reduceRight) Array.prototype.reduceRight = function(f, v){ var i = this.length - 1; if(v == null){v = this[i]; i--;} for(i; i>=0; i--){ if(this[i] === undefined) continue; v = f.call(null, v, this[i], i, this); } return v; }; /* ========== Statistics ========== */ stat = (function(){ var sum = function(a){ return a.reduce(function(v, elm){return v + elm;}); }; var average = function(data){return sum(data) / data.length;}; var variance = function(data){ var av = average(data); return sum(data.map(function(elm){return Math.pow(elm - av, 2);})) / data.length; }; var sd = function(data){return Math.sqrt(variance(data));}; return { average: average, variance: variance, sd: sd }; })(); /* ========== def Random functions ========== */ random = {}; random.jsn = function(n){ var r = new Array(n); for(var i=0; i<n; i++) r[i] = Math.random(); return r; }; random.lcg = function(n, s, a){ var tf = true; if(!n){n = 100; tf = false;} if(!s) s = (new Date()).getTime(); if(!a) a = new Array(421, 3, 1e+16); var suc = function(x){return (a[0] * x + a[1]) % a[2];}; var r = new Array(n); for(var i=0; i<n; i++){ s = suc(s); r[i] = s / 1e+16; } return (tf) ? r : r[99]; }; /* ========== Test code ========== */ (function(){ var rTest = function(_round){ var doTest = function(f){ var time1 = (new Date()).getTime(); var arr = f.call(null, _round); var time2 = (new Date()).getTime(); arr = arr.map(function(elm){return Math.floor(elm * 10);}); var am = [0,0,0,0,0,0,0,0,0,0]; arr.forEach(function(elm){++am[elm];}); return { t: time2 - time1, data: am, v: stat.variance(am), sd: stat.sd(am) }; }; var printTest = function(f){ var o = doTest(f); document.write("<td>" + o.v + "</td>"); document.write("<td>" + o.sd + "</td>"); document.write("<td>" + o.t + "</td>"); document.write("<td>" + o.data.join(", ") + "</td>"); }; document.open(); document.write("round = " + _round + "<br/>"); document.write("<table border='1'><tr><th></th><th>variance</th><th>standerd deviation</th><th>time</th><th>DATA</th></tr>"); document.write("<tr><th>JS Native</th>"); printTest(random.jsn); document.write("</tr><tr><th>LCG</th>"); printTest(random.lcg); document.write("</tr></table>"); document.close(); }; //rTest(10000); rTest(50000); rTest(100000); rTest(1000000); })(); </script></body></html>