himorogiの日記

主にプログラミングに関することなど。少々ハード(電子工作)についても。

bit 演算版 lotos() 関数リテラルで定義 ver.

Java6 上の jrunscript 用

var lotos =function (depth){
  var ceil, mask=[0,0], coll =[];

  function getNum(){ return(function(x){ return x.substr(x.length-2,2) })("00"+ Math.ceil(Math.random()*ceil)); };
  isnotUniq= function(x) { return 0!=(mask[Math.floor(x/32)] &  1<<(x%32)); };
  pushNum= function(x) { mask[Math.floor(x/32)] |= 1<<(x%32); coll.push(x); }

  ceil=(function(x){for(i=c=0;i<x; c+= ++i);return c})(depth)*2+1;
  pushNum(getNum());
  while( coll.length<depth ){ pushNum((function(){ while(isnotUniq(x=getNum())); return x; })()); }
  return coll;
};

sx=read("5:minilot\n6:loto6\n");
if(sx<5 || sx>6){ print("bad arguments\n"); }else{ println(lotos(sx).sort().toString()); }

※ 20:11 2008/04/13 追記:getNum()、putNum() 修正

coll を lotos のメンバとして、直接 lotos(sx).coll.sort().toString() で結果を参照しようとしたが、上手く行かない。
※ 20:05 2008/04/13 追記:JDK 1.6.0.5 で確認

var lotos =function (depth){
  var ceil, mask=[0,0]
  this.coll =[];

  function getNum(){ return(function(x){ return x.substr(x.length-2,2) })("00"+ Math.ceil(Math.random()*ceil)); };
  isnotUniq= function(x) { return 0!=(mask[Math.floor(x/32)] &  1<<(x%32)); };
  pushNum= function(x) { mask[Math.floor(x/32)] |= 1<<(x%32); coll.push(x); }

  ceil=(function(x){for(i=c=0;i<x; c+= ++i);return c})(depth)*2+1;
  pushNum(getNum());
  while( coll.length<depth ){ pushNum((function(){ while(isnotUniq(x=getNum())); return x; })()); }
};

sx=read("5:minilot\n6:loto6\n");
if(sx<5 || sx>6){ print("bad arguments\n"); }else{ println(lotos(sx).coll.sort().toString()); }

※ 20:11 2008/04/13 追記:getNum()、putNum() 修正

そのときの error message ※ kuji.js は script file 名

script error: sun.org.mozilla.javascript.internal.WrappedException: Wrapped javax.script.ScriptException: sun.org.mozill
a.javascript.internal.EcmaError: TypeError: Cannot read property "coll" from undefined (kuji.js#15) in kuji.js at line n
umber 15 (<system-init>#294) in <system-init> at line number 294
js>