Math.random() is not really random on Android

I have card game where I shuffle cards randomly using Math.random().
It works fine for Flash and Windows target, but on Android target from time to time it returns the same not random results.
Any help with that?
Thank you!

At the beggining of your shuffle function just add:

#if cpp
untyped __cpp__('srand(time(NULL))');

and add


in the header of your class containing that function

Wow! Thanks guys for quick help! :slight_smile:

Perhaps there’s a way to improve this?

Probably need to be fixed somehow, it’s not good that standard library version of random works incorrectly

I use Random library.

haxelib install Random

It’s a small class but solves the problem of output repeating random numbers especially inside a loop.

Yes, I know that alternatives exists, but it’s incorrect behavior in standart library, that will be used by many developers and it’s bad

Has anyone filed a bug report for hxcpp? Since it’s not a bug in OpenFL, but in a lower level dependency, this thread probably isn’t going to be seen by whoever can fix the issue.

Yes, already there
Using srand48 instead of srand helps for me

1 Like