haXe在flash平台中没有可设置种子的随机数产生器,cpp平台中有cpp.Random类可设置种子,但不能用于flash平台,比较麻烦。
这里特别给出一个各平台通用的“有种的”随机数产生器类:
package net.cnjm.haxe.util;
#if cpp
typedef Random = cpp.Random;
#else
import nme.display.BitmapData;
/**
* ...
* @author Rocks Wang
*/
class Random {
private var _seed: Int;
private var _pointer: Int;
private var bmpd: BitmapData;
private var seedInvalid: Bool;
public function new() {
_seed = Std.random(2147483648);
_pointer = 0;
bmpd = new BitmapData(1000, 200);
seedInvalid = true;
}
public function setSeed(s: Int) : Void {
if (s != _seed) {
seedInvalid = true;
_pointer = 0;
}
_seed = s;
}
public function float() : Float {
if (seedInvalid) {
bmpd.noise(_seed, 0, 255, 1|2|4|8);
seedInvalid = false;
}
_pointer = (_pointer + 1) % 200000;
// Flash's numeric precision appears to run to 0.9999999999999999, but we'll drop one digit to be safe:
return (bmpd.getPixel32(_pointer % 1000, Std.int(_pointer / 1000)) * 0.999999999999998 + 0.000000000000001) / 4294967295.0;
}
public inline function int(max: Int) : Int {
return Std.int(float() * max);
}
}
#end
注意,使用BitmapData.noise()来产生随机数的算法是我从网上找的老外的AS开源代码,非本人原创,特此声明。