Skip to content

Instantly share code, notes, and snippets.

@jcward
Last active August 29, 2015 13:56
Show Gist options
  • Save jcward/9220172 to your computer and use it in GitHub Desktop.
Save jcward/9220172 to your computer and use it in GitHub Desktop.
Haxe cross-platform rand() function that provides a full 32-bit random Ints
// Author: Jeff Ward
// License: MIT
//
// Haxe Std.random() can only generate 31 bits of random data. This seems to
// be a Neko limitation that is foisted on the other platforms. Well, no
// longer! cpp, flash, and js are liberated below.
package;
class Rand32 {
#if (flash)
public inline static var _rand_max:UInt = 0xffffffff;
#end
// 32-bit random int
public static inline function rand():Int {
#if (cpp)
return untyped __global__.__hxcpp_irand(0xffffffff);
#elseif (flash)
//return untyped Math.random()*_rand_max;
return untyped Math.random()*_rand_max;
#elseif (js)
return untyped __js__("Math.random()*0xffffffff|0");
#else
return Std.random(0x10000)<<16|Std.random(0x10000);
#end
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment