乱数とテストコード

ゲームの処理なんかで、乱数を使う場合、乱数の値によって処理が変わってしまうので、テストがしづらい。
とりあえず、乱数発生用の処理を

class GRand extends CComponent {
public static $counter = 0;
public static $list = null;

static public function ready( $list ) {
self::$list = $list;
self::$counter = 0;
}

static public function rand($min,$max) {
if ($min>$max) {
throw "min is larger than max";
}
if (isset(self::$list)) {
$x = self::$list[self::$counter++];
while ($max<$x) {
$x -= ($max-$min+1);
}
while ($x<$min) {
$x += ($max-$min+1);
}
return $x;
}
return rand($min,$max);
}



}

のようにして、普段の乱数使用は

$r = GRand::rand(1,100);

のように記述。テストコードから呼び出すときは、

GRand::ready( array( 50 , 20 , 10 ) );

として、乱数の値が順に50,20,10で発生した場合を想定するような感じで作ってみた。