A class containing a set of functions for random generation. Should be accessed via FlxG.random.

Constructor

new(?InitialSeed:Int)

Create a new FlxRandom object.

Parameters:

InitialSeed

The first seed of this FlxRandom object. If ignored, a random seed will be generated.

Variables

currentSeed:Int

Current seed used to generate new random numbers. You can retrieve this value if, for example, you want to store the seed that was used to randomly generate a level.

@:value(1)initialSeed:Int = 1

The global base random number generator seed (for deterministic behavior in recordings and saves). If you want, you can set the seed with an integer between 1 and 2,147,483,647 inclusive. Altering this yourself may break recording functionality!

Methods

@:value({ Chance : 50 })inlinebool(Chance:Float = 50):Bool

Returns true or false based on the chance value (default 50%). For example if you wanted a player to have a 30.5% chance of getting a bonus, call bool(30.5) - true means the chance passed, false means it failed.

Parameters:

Chance

The chance of receiving the value. Should be given as a number between 0 and 100 (effectively 0% to 100%)

Returns:

Whether the roll passed or not.

@:value({ GreyScale : false })color(?Min:FlxColor, ?Max:FlxColor, ?Alpha:Int, GreyScale:Bool = false):FlxColor

Returns a random color.

Parameters:

Min

An optional FlxColor representing the lower bounds for the generated color.

Max

An optional FlxColor representing the upper bounds for the generated color.

Alpha

An optional value for the alpha channel of the generated color.

GreyScale

Whether or not to create a color that is strictly a shade of grey. False by default.

Returns:

A color value as a FlxColor.

@:value({ Max : 1, Min : 0 })float(Min:Float = 0, Max:Float = 1, ?Excludes:Array<Float>):Float

Returns a pseudorandom float value between Min (inclusive) and Max (exclusive). Will not return a number in the Excludes array, if provided. Please note that large Excludes arrays can slow calculations.

Parameters:

Min

The minimum value that should be returned. 0 by default.

Max

The maximum value that should be returned. 1 by default.

Excludes

Optional array of values that should not be returned.

@:value({ StdDev : 1, Mean : 0 })floatNormal(Mean:Float = 0, StdDev:Float = 1):Float

Returns a pseudorandom float value in a statistical normal distribution centered on Mean with a standard deviation size of StdDev. (This uses the Box-Muller transform algorithm for gaussian pseudorandom numbers)

Normal distribution: 68% values are within 1 standard deviation, 95% are in 2 StdDevs, 99% in 3 StdDevs. See this image: https://github.com/HaxeFlixel/flixel-demos/blob/dev/Performance/FlxRandom/normaldistribution.png

Parameters:

Mean

The Mean around which the normal distribution is centered

StdDev

Size of the standard deviation

@:value({ StartIndex : 0 })@:genericgetObject<T>(Objects:Array<T>, ?WeightsArray:Array<Float>, StartIndex:Int = 0, ?EndIndex:Int):T

Returns a random object from an array.

Parameters:

Objects

An array from which to return an object.

WeightsArray

Optional array of weights which will determine the likelihood of returning a given value from Objects. If none is passed, all objects in the Objects array will have an equal likelihood of being returned.

                    Values in WeightsArray will correspond to objects in Objects exactly.
StartIndex

Optional index from which to restrict selection. Default value is 0, or the beginning of the Objects array.

EndIndex

Optional index at which to restrict selection. Ignored if 0, which is the default value.

Returns:

A pseudorandomly chosen object from Objects.

@:value({ Max : FlxMath.MAX_VALUE_INT, Min : 0 })int(Min:Int = 0, Max:Int = FlxMath.MAX_VALUE_INT, ?Excludes:Array<Int>):Int

Returns a pseudorandom integer between Min and Max, inclusive. Will not return a number in the Excludes array, if provided. Please note that large Excludes arrays can slow calculations.

Parameters:

Min

The minimum value that should be returned. 0 by default.

Max

The maximum value that should be returned. 2,147,483,647 by default.

Excludes

Optional array of values that should not be returned.

inlineresetInitialSeed():Int

Function to easily set the global seed to a new random number. Please note that this function is not deterministic! If you call it in your game, recording may not function as expected.

Returns:

The new initial seed.

@:genericshuffle<T>(array:Array<T>):Void

Shuffles the entries in an array in-place into a new pseudorandom order, using the standard Fisher-Yates shuffle algorithm.

Parameters:

array

The array to shuffle.

Available since

4.2.0

.

@:genericshuffleArray<T>(Objects:Array<T>, HowManyTimes:Int):Array<T>

Deprecated: "Unless you rely on reproducing the exact output of shuffleArray(), you should use shuffle() instead, which is both faster and higher quality."

Shuffles the entries in an array into a new pseudorandom order.

Parameters:

Objects

An array to shuffle.

HowManyTimes

How many swaps to perform during the shuffle operation. A good rule of thumb is 2-4 times the number of objects in the list.

Returns:

The newly shuffled array.

@:value({ Chance : 50 })inlinesign(Chance:Float = 50):Int

Returns either a 1 or -1.

Parameters:

Chance

The chance of receiving a positive value. Should be given as a number between 0 and 100 (effectively 0% to 100%)

Returns:

1 or -1

weightedPick(WeightsArray:Array<Float>):Int

Pseudorandomly select from an array of weighted options. For example, if you passed in an array of [50, 30, 20] there would be a 50% chance of returning a 0, a 30% chance of returning a 1, and a 20% chance of returning a 2. Note that the values in the array do not have to add to 100 or any other number. The percent chance will be equal to a given value in the array divided by the total of all values in the array.

Parameters:

WeightsArray

An array of weights.

Returns:

A value between 0 and (SelectionArray.length - 1), with a probability equivalent to the values in SelectionArray.