HaxeFlixel/flixel — pull request #747 by steverichey (2013-12-27 05:54:51+00:00)
Title: Update FlxRandom, add new FlxRandom functions, and ensure consistent use of FlxRandom

This pull request significantly overhauls the way FlxRandom works for consistency, especially in recordings. FlxG now calls `FlxRandom.resetGlobalSeed();` during game creation to set a new random seed. All calls to FlxRandom will now use this seed; previously, some functions used this seed while others use Math.random(), and still others used their own seed.

After extensive review I believe that the best pseudorandom number generation equation for HaxeFlixel is a linear congruential generator using the same constants as Microsoft Visual Basic 6. I chose these constants because they provide a wide array of pseudorandom numbers, have been in use in a very common programming system, and can be used to provide values that are always greater than zero without exceeding the word length of Haxe's 31-bit integers. These values can be seen as `MULTIPLIER`, `INCREMENT`, and `MODULUS`.

I've also made each function simpler and easier to read. This update maintains the recent additions to `FlxRandom` allowing Float values for `chanceRoll` and `sign`.

Per @gama11's recommendation, I've removed `randomColor` FlxColorUtil, as it was redundant to a similar function in `FlxRandom`. I've also added a `weightedPick` function that is helpful if you'd like to randomly select a value, say from an array.

The most expansive change updates all classes in HaxeFlixel to use `FlxRandom` instead of `Math.random`, and use `FlxRandom`'s functions as necessary.

Early tests show this to be quite fast (1,000,000 random floats in 20ms in Flash, 7ms in Windows) and I've ensured that all functions perform as expected (i.e. `intRanged` and `floatRanged` respect the Exclude arrays, `chanceRoll` works for given Chance values) but I do welcome any requests for more extensive tests or benchmarks.

Please review and comment judiciously; I don't want to break HaxeFlixel! Thank you.