[OpenFL Cpp] simple pseudorandom algo works differently with/without "-debug"

55 views
Skip to first unread message

Rezmason

unread,
Jun 22, 2014, 4:30:26 PM6/22/14
to haxe...@googlegroups.com
Hey folks! I've tried writing a simple function to generate sorta random Floats between 0 and 1:
function lgm(n:UInt):Void->Float {
return function() {
n = (n * 0x41A7) % 0x7FFFFFFF;
return n / 0x7FFFFFFF;
}
}

I'm using this function in an OpenFL project that I'm compiling for the Mac. When I compile the project with the "-debug" flag, this function spits out numbers between 0 and 1, as expected. But when I compile the project without the "-debug" flag, two thirds of the numbers are between 0 and 1, and one third of the numbers are between 1 and 2!

Could someone explain to me what's going on behind the scenes here? The C++ file generated for the class this function sits in looks the exact same for both builds, so, I'm at a loss.

Rezmason

unread,
Jun 22, 2014, 4:32:08 PM6/22/14
to haxe...@googlegroups.com
...That looks nothing like code, does it. I'll try again:

Rezmason

unread,
Jun 22, 2014, 5:34:13 PM6/22/14
to haxe...@googlegroups.com
Ah, this seems to fix it! I've stored the literals in variables of type UInt:

function lgm(n:UInt):Void->Float {
    var a:UInt = 0x41A7;
    var div:UInt = 0x7FFFFFFF;
    return function() {
        n = (n * a) % div;
        return n / div;
    }
}


Hugh

unread,
Jun 23, 2014, 12:37:48 AM6/23/14
to haxe...@googlegroups.com
Just a comment - not saying you are wrong - but I would think the "&" operator would be more normal than the "%" operator here (and then divide by 0x8000000)


Hugh

Rezmason

unread,
Jun 23, 2014, 10:14:53 AM6/23/14
to haxe...@googlegroups.com
On Sunday, June 22, 2014 9:37:48 PM UTC-7, Hugh wrote:
Just a comment - not saying you are wrong - but I would think the "&" operator would be more normal than the "%" operator here (and then divide by 0x8000000)


Hugh


Noted! I haven't even checked yet whether my algo generates the same values from one platform to the next- it's a placeholder, until then. :-)
Reply all
Reply to author
Forward
0 new messages