Skip to content

Instantly share code, notes, and snippets.

@IQAndreas
Last active August 29, 2015 14:01
Show Gist options
  • Save IQAndreas/1e2422b1f0ed95e41936 to your computer and use it in GitHub Desktop.
Save IQAndreas/1e2422b1f0ed95e41936 to your computer and use it in GitHub Desktop.
Inconsistent Math results between AS3 and Haxe->AS3. See https://groups.google.com/forum/#!msg/haxelang/GHqaYCea8fE/5TJi7Mio0ngJ
package
{
import flash.text.TextField;
import flash.display.Sprite;
public class Main extends Sprite
{
public function Main()
{
traceVisual("TEST #5");
var rnd:Random = new Random(256);
for (var i:int = 0; i < 15; i++)
{
traceVisual(rnd.float());
}
}
private var tf:TextField;
private function initVisual():void
{
tf = new TextField();
tf.width = stage.stageWidth;
tf.height = stage.stageHeight;
stage.addChild(tf);
}
private function traceVisual(str:*):void
{
if (!tf) { initVisual(); }
tf.text += String(str) + "\n";
}
}
}
class Random
{
public function Random(seed:uint = 0)
{
_seed = seed;
}
// Internal tracker that keeps track of the current value of the seed as it changes with each iteration
protected var _seed:int = 0;
/**
* Constants used in the pseudorandom number generation equation.
* These are the constants suggested by the revised MINSTD pseudorandom number generator, and they use the full range of possible integer values.
*
* @see http://en.wikipedia.org/wiki/Linear_congruential_generator
* @see Stephen K. Park and Keith W. Miller and Paul K. Stockmeyer (1988). "Technical Correspondence". Communications of the ACM 36 (7): 105–110.
*/
protected const MULTIPLIER:int = 48271;
protected const MODULUS:int = 2147483647; // 0x7FFFFFFF (31 bit integer)
protected function generate():int
{
return _seed = ((_seed * MULTIPLIER) % MODULUS) & MODULUS;
//return (_seed / MODULUS);
}
public function float():Number
{
return generate() / MODULUS;
}
}
package;
class Main
{
public static function main()
{
trace("TEST #5");
var rnd = new Random(256);
for (i in 0...15)
{
trace(rnd.float());
}
}
}
class Random
{
public function new(seed:Int)
{
_seed = seed;
}
private static var _seed:Int = 1;
/**
* Constants used in the pseudorandom number generation equation.
* These are the constants suggested by the revised MINSTD pseudorandom number generator, and they use the full range of possible integer values.
*
* @see http://en.wikipedia.org/wiki/Linear_congruential_generator
* @see Stephen K. Park and Keith W. Miller and Paul K. Stockmeyer (1988). "Technical Correspondence". Communications of the ACM 36 (7): 105–110.
*/
private static inline var MULTIPLIER:Int = 48271;
private static inline var MODULUS:Int = 2147483647;
private inline function generate():Int
{
return _seed = ((_seed * MULTIPLIER) % MODULUS) & MODULUS;
}
public inline function float():Float
{
return generate() / MODULUS;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment