
HGE Random
1
По просьбам трудящихся выкладываю код рендома из движка HGE, портированный на AS3, haXe и ObjC.
AS3
haXe
Objective-C++
AS3
package elmortem.utils {
public class RandUtils {
static private var pSeed:uint;
static public function set seed(val:uint):void {
if (val != 0) pSeed = val;
else pSeed = uint(Math.random() * uint.MAX_VALUE);
}
static public function get seed():uint {
return pSeed;
}
static public function getInt(min:int, max:int):int {
pSeed = 214013 * pSeed + 2531011;
return min + (pSeed ^ (pSeed >> 15)) % (max - min + 1);
}
static public function getFloat(min:Number, max:Number):Number {
pSeed = 214013 * pSeed + 2531011;
return min + (pSeed >>> 16) * (1.0 / 65535.0) * (max - min);
}
}
}
haXe
package elm.utils;
class Rand {
public static var seed(default, null):UInt;
public static function setSeed(val:UInt):Void {
if (val != 0) seed = val;
else seed = Math.floor(Math.random()*Math.POSITIVE_INFINITY);
}
public static function getInt(min:Int, max:Int):Int {
seed = 214013 * seed + 2531011;
return min+(seed ^ (seed>>15))%(max-min+1);
}
public static function getFloat(min:Float, max:Float):Float {
seed = 214013 * seed + 2531011;
return min + (seed>>>16) * (1.0 / 65535.0) * (max - min);
}
}
Objective-C++
@implementation RandUtils
static uint gSeed = 0;
+(void) randomSeed:(int)seed {
if(!seed) gSeed = time(NULL);
else gSeed = seed;
}
+(int) randomInt:(int)min max:(int)max {
gSeed = 214013 * gSeed + 2531011;
return min + (gSeed ^ gSeed >> 15) % (max - min + 1);
}
+(float)randomFloat:(float)min max:(float) max {
gSeed = 214013 * gSeed + 2531011;
return min + (gSeed >> 16) * (1.0f / 65535.0f) * (max - min);
}
@end
- +15
- elmortem
Комментарии (1)