himorogiの日記

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

Numbers

■□WindowsScriptHost で Numbers4 の数字を生成する script を以前書いていた

var str="000"+Math.floor(Math.random()*10000);
WScript.Echo(str.substr(str.length-4,4));

■□その前のオリジナルでは Math.pow を使っていた(実行環境を変えたとき、script をコピーしなかったので)

var str=String(Math.pow(10,3))+Math.floor(Math.random()*Math.pow(10,4));
WScript.Echo(str.substr(str.length-4,4));

■□関数版⇒引数に 3 を渡せば Numbers3、4 を渡せば Numbers4 の数字を生成する

function numbers(nx){
 var s=String(Math.pow(10,nx-1)) + Math.floor(Math.random()*Math.pow(10,nx));
 return(s.substr(s.length-nx,nx));
}