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


class GKShuffledDistribution : GKRandomDistribution


The behavior of a shuffled distribution is sometimes called “fair” randomization, because true randomness in games can result in extended “lucky streaks” or “unlucky streaks” for players. To create a shuffled distribution and use it to generate random numbers, use the methods defined by its superclass GKRandomDistribution.

The GKShuffledDistribution class inherits its entire interface from its superclass—to initialize and use a shuffled distribution, use the methods listed in GKRandomDistribution. A shuffled distribution differs from its superclass in behavior only. Consider the code snippets below:

// Uniform distribution
let uniform = GKRandomDistribution.d6()
for _ in 1...100 { print(uniform.nextInt()) }
// Shuffled distribution
let shuffled = GKShuffledDistribution.d6()
for _ in 1...100 { print(shuffled.nextInt()) }

In this example, each distribution generates 100 random integers from a simulated six-sided die. In both cases, the distribution of results is roughly uniform—that is, the number of occurrences of any specific value is about the same as that of any other value. However, the shuffled distribution makes sure not to repeat any one value until it has used all of its possible values. In this example, if the die rolls a 1, the shuffled distribution will not generate another 1 for at least five more rolls.

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


Conforms To

See Also


protocol GKRandom

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

class GKRandomSource

The superclass for all basic randomization classes in GameplayKit.

class GKARC4RandomSource

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

class GKLinearCongruentialRandomSource

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

class GKMersenneTwisterRandomSource

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

class GKRandomDistribution

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

class GKGaussianDistribution

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