Last active
August 29, 2015 13:56
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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