himorogiの日記

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

Javascript for Automation で LotoNumbers を生成

JavascriptVisualBasic の Left 関数に相当する機能がなくて不便なので prototype 宣言した

app = Application.currentApplication();
app.includeStandardAdditions =true;

String.prototype.left = function(l){ return this.substr( (this.length > l)?(this.length-l):0,(this.length >= l)?l:this.length -1 ) }

miniloto = [];
miniloto.push( ( "00" + Math.ceil( Math.random()*31 )).left( 2 ) );
do{
	a = ( "00" + Math.ceil( Math.random()*31 )).left( 2 ); 
	for( var i = 0; i < miniloto.length; i++ ){
		done = ( a == miniloto[i] );
		if( done ) break;
	}
	if( !done ) miniloto.push( a );
}while( miniloto.length < 5 );


loto6 = [];
loto6.push( ( "00" + Math.ceil( Math.random()*43 )).left( 2 ) );
do{
	a = ( "00" + Math.ceil( Math.random()*43 )).left( 2 ); 
	for( var i = 0; i < loto6.length; i++ ){
		done = ( a == loto6[i] );
		if( done ) break;
	}
	if( !done ) loto6.push( a );
}while( loto6.length < 6 );


loto7 = [];
loto7.push( ( "00" + Math.ceil( Math.random()*37 )).left( 2 ) );
do{
	a = ( "00" + Math.ceil( Math.random()*37 )).left( 2 ); 
	for( var i = 0; i < loto7.length; i++ ){
		done = ( a == loto7[i] );
		if( done ) break;
	}
	if( !done ) loto7.push( a );
}while( loto7.length < 7 );

app.displayAlert( miniloto.sort().toString() );
app.displayAlert( loto6.sort().toString() );
app.displayAlert( loto7.sort().toString() );

displayAlert 3連発になったのは displayAlert に改行コード('\n')送っても改行されないので…

Javascript for Automation は ECMAScript5 の機能が使えるので、配列要素検索を indexOf で書き換えた

app = Application.currentApplication();
app.includeStandardAdditions =true;

String.prototype.left = function(l){ return this.substr( (this.length > l)?(this.length-l):0,(this.length >= l)?l:this.length -1 ) }

miniloto = [];
miniloto.push( ( "00" + Math.ceil( Math.random()*31 )).left( 2 ) );
do{
	a = ( "00" + Math.ceil( Math.random()*31 )).left( 2 ); 
	if( miniloto.indexOf( a )<0 ) miniloto.push( a );
}while( miniloto.length < 5 );


loto6 = [];
loto6.push( ( "00" + Math.ceil( Math.random()*43 )).left( 2 ) );
do{
	a = ( "00" + Math.ceil( Math.random()*43 )).left( 2 ); 
	if( ( loto6.indexOf( a )<0 ) ) loto6.push( a );
}while( loto6.length < 6 );


loto7 = [];
loto7.push( ( "00" + Math.ceil( Math.random()*37 )).left( 2 ) );
do{
	a = ( "00" + Math.ceil( Math.random()*37 )).left( 2 ); 
	if( ( loto7.indexOf( a )<0 ) ) loto7.push( a );
}while( loto7.length < 7 );

app.displayAlert( miniloto.sort().toString() );
app.displayAlert( loto6.sort().toString() );
app.displayAlert( loto7.sort().toString() );

Javascript for Automation のイマイチなところ

うっかり無限ループになるような script を Script Editor で実行すると、キーボードショートカットやメニューから停止できない。
しょうがないのでアクティビティモニターからプロセス停止しているけど、使い方がわるいのだろうか?

substr( -n ) で left関数の代用ができるというか、第二パラメータ省略できたのか…知らんかった。ついでに do{…}while() を for に置き換えた

app = Application.currentApplication();
app.includeStandardAdditions =true;

function lotoGen(m){ return ("00" + Math.ceil( Math.random()*m )).substr(-2) }

function loto(n,o){
var	numbers = []; 
	for( numbers.push( lotoGen( o ) ); numbers.length < n; ){
		if( numbers.indexOf( a = lotoGen( o ) )<0 ) numbers.push( a );
	};
	return numbers;
}

app.displayAlert( loto( 5, 31 ).sort().toString() );
app.displayAlert( loto( 6, 43 ).sort().toString() );
app.displayAlert( loto( 7, 37 ).sort().toString() );