Class

GKRandomDistribution

A generator for random numbers that fall within a specific range and that exhibit a specific distribution over multiple samplings.

Overview

You choose the algorithm that randomizes source values for a distribution by initializing it with an instance of any class that implements the GKRandom protocol, such as a basic random source (a subclass of GKRandomSource) or another random distribution. The GKRandomDistribution class itself implements a uniform distribution—for more specialized distributions use one of the subclasses GKGaussianDistribution and GKShuffledDistribution.

In a uniform distribution, the probability of generating any number in a specified range (between the values of the distribution’s lowestValue and highestValue properties) is approximately equal. In other words, there is no bias toward any possible outcome. To generate random numbers in this range, use the methods from the GKRandom protocol listed in Generating Random Numbers.

For more information on choosing and using randomizers in GameplayKit, read Randomization in GameplayKit Programming Guide.

Topics

Creating a Random Distribution

- initWithRandomSource:lowestValue:highestValue:

Initializes a uniform random distribution with the specified lower and upper bounds, using the specified source randomizer.

+ distributionWithLowestValue:highestValue:

Creates a random distribution with the specified lower and upper bounds, using the Arc4 randomizer.

Creating Specific Random Distributions

+ d6

Creates a random distribution equivalent to a six-sided die.

+ d20

Creates a random distribution equivalent to a twenty-sided die.

+ distributionForDieWithSideCount:

Creates a random distribution equivalent to a die with the specified number of sides.

Generating Random Numbers

- nextInt

Generates and returns a new random integer within the bounds of the distribution.

- nextIntWithUpperBound:

Generates and returns a new random integer within the bounds of the distribution and less than the specified limit.

- nextUniform

Generates and returns a new random floating-point value within the characteristics of the distribution.

- nextBool

Generates and returns a new random Boolean value within the characteristics of the distribution.

Working with Characteristics of a Distribution

lowestValue

The lowest value to be produced by the distribution.

highestValue

The highest value to be produced by the distribution.

numberOfPossibleOutcomes

The number of unique values the distribution can generate.

Relationships

Randomization

GKRandom

The common interface for all randomization classes in (or usable with) GameplayKit.

GKRandomSource

The superclass for all basic randomization classes in GameplayKit.

GKARC4RandomSource

A basic random number generator implementing the ARC4 algorithm, which is suitable for most gameplay mechanics.

GKLinearCongruentialRandomSource

A basic random number generator implementing the linear congruential generator algorithm, which is faster but less random than the default random source.

GKMersenneTwisterRandomSource

A basic random number generator implementing the Mersenne Twister algorithm, which is more random, but slower than the default random source.

GKGaussianDistribution

A generator for random numbers that follow a Gaussian distribution (also known as a normal distribution) across multiple samplings.

GKShuffledDistribution

A generator for random numbers that are uniformly distributed across many samplings, but where short sequences of similar values are unlikely.