class flixel.util.FlxRandom

Available on all platforms

A class containing a set of functions for random generation.

Class Fields

static var globalSeed:Int

The global 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. However, FlxG automatically sets this with a new random seed when starting your game. Altering this yourself may break recording functionality!

static function chanceRoll(?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% chance of getting a bonus, call chanceRoll(30) - true means the chance passed, false means it failed. * *

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.

static function color(?Min:Int = 0, ?Max:Int = 255, ?Alpha:Int = 255, ?GreyScale:Bool = false):Int

Returns a random color value in hex ARGB format. * *

Min

The lowest value to use for each channel. *

Max

The highest value to use for each channel. *

Alpha

The alpha value of the returning color (default 255 = fully opaque). *

GreyScale

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

returns

A color value in hex ARGB format.

static function colorExt(?RedMinimum:Int = 0, ?RedMaximum:Int = 255, ?GreenMinimum:Int = 0, ?GreenMaximum:Int = 255, ?BlueMinimum:Int = 0, ?BlueMaximum:Int = 255, ?AlphaMinimum:Int = -1, ?AlphaMaximum:Int = -1):Int

Much like color(), but with much finer control over the output color. * *

RedMinimum

The minimum amount of red in the output color, from 0 to 255. *

RedMaximum

The maximum amount of red in the output color, from 0 to 255. *

GreedMinimum

The minimum amount of green in the output color, from 0 to 255. *

GreenMaximum

The maximum amount of green in the output color, from 0 to 255. *

BlueMinimum

The minimum amount of blue in the output color, from 0 to 255. *

BlueMaximum

The maximum amount of blue in the output color, from 0 to 255. *

AlphaMinimum

The minimum alpha value for the output color, from 0 (fully transparent) to 255 (fully opaque). Set to -1 or ignore for the output to be always fully opaque. *

AlphaMaximum

The maximum alpha value for the output color, from 0 (fully transparent) to 255 (fully opaque). Set to -1 or ignore for the output to be always fully opaque. *

returns

A pseudorandomly generated color within the ranges specified.

static function float():Float

Returns a pseudorandom number between 0 and 1, inclusive.

static function floatRanged(?Min:Float = 0, ?Max:Float = 1, ?Excludes:Array<Float> = null):Float

Returns a pseudorandom float value 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. * *

Min

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

Max

The maximum value that should be returned. 33,554,429 by default. *

?Excludes

An optional array of values that should not be returned.

static function getObject<T>(Objects:Array<T>, ?StartIndex:Int = 0, ?EndIndex:Int = 0):T

Fetch a random entry from the given array from StartIndex to EndIndex. * Will return null if random selection is missing, or array has no entries. * *

Objects

An array from which to select a random entry. *

StartIndex

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

EndIndex

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

returns

The random object that was selected.

static function getObject_getRandom_T(Objects:Array<T>, ?StartIndex:Int = 0, ?EndIndex:Int = 0):T

static function int():Int

Returns a pseudorandom number between 0 and 2,147,483,647, inclusive.

static function intRanged(?Min:Int = 0, ?Max:Int = 2147483647, ?Excludes:Array<Int> = null):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. * *

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

An optional array of values that should not be returned.

static function resetGlobalSeed():Int

Function to easily set the global seed to a new random number. Used primarily by FlxG whenever the game is reset. * Please note that this function is not deterministic! If you call it in your game, recording may not work. * *

returns

The new global seed.

static function shuffleArray<T>(Objects:Array<T>, HowManyTimes:Int):Array<T>

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

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.

static function shuffleArray_shuffle_T(Objects:Array<T>, HowManyTimes:Int):Array<T>

static function sign(?Chance:Float = 50):Int

Returns either a 1 or -1. * *

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

static function weightedGetObject<T>(Objects:Array<T>, WeightsArray:Array<Float>, ?StartIndex:Int = 0, ?EndIndex:Int = 0):T

Returns a random object from an array between StartIndex and EndIndex with a weighted chance from WeightsArray. * This function is essentially a combination of weightedPick and getObject. * *

Objects

An array from which to return an object. *

WeightsArray

An array of weights which will determine the likelihood of returning a given value from Objects. 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.

static function 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. * *

WeightsArray

An array of weights. *

returns

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