c4se記:さっちゃんですよ☆

.。oO(さっちゃんですよヾ(〃l _ l)ノ゙☆)

.。oO(此のblogは、主に音樂考察Programming に分類されますよ。ヾ(〃l _ l)ノ゙♬♪♡)

音樂は SoundCloud に公開中です。

考察は現在は主に Scrapbox で公表中です。

Programming は GitHub で開發中です。

#js 乱数 - 線形合同法


だいぶせまった。


前回→乱数生成
 http://d.hatena.ne.jp/Kureduki_Maari/20090106/1231229714


使ったコード(テスト含む)

<html><body><script type="application/javascript">
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;
};

yMath = (function(){// Linear congruential generators, LCGs
  //var a, b, m; [a, b, m] = [76943*78479*8747*4*5+1, 997279*997307, 76943*78479*78479*4*5];
  var succ = function(x){return ((1056359392393181 * x + 994593327653) % 9477767092217260);};
  var lcgs = function(x, n){
    var a = new Array();
    for(var i=0; i<n; i++){
      x = succ(x);
      a[a.length] = x / 1e+16;
    }
    return a;
  };
  
  return {
    random: function(n, x){
      if(n == null) n = 1;
      if(x == null) x = (new Date().getTime());
      return (n==1? lcgs(x, n)[0]: lcgs(x, n));
    }
  };
})();

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
  };
})();

round = 100000;
ya = yMath.random(round);
ya2 = (function(){
  var a = new Array(round);
  for(var i=0; i<round; i++)
    a[i] = yMath.random();
  return a;
})();
ja = (function(){
  var a = new Array(round);
  for(var i=0; i<round; i++)
    a[i] = Math.random();
  return a;
})();
printTest = function(arr){
  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]++;});
  arr = am; am = null;
  document.write("[" + arr.join(", ") + "]<br/>");
  document.write("average: " + stat.average(arr) + "<br/>");
  document.write("variance: " + stat.variance(arr) + "<br/>");
  document.write("sd: " + stat.sd(arr) + "<br/>");
};

document.open();
document.write("yMath.random()<br/>");
printTest(ya);
document.write("<br/>劣等yMath.random()<br/>");
printTest(ya2);
document.write("<br/>Math.random()<br/>");
printTest(ja);
document.close();
</script></body></html>

実行ブラウザはOpera 10.00 alpha。