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


class GKARC4RandomSource : GKRandomSource


To generate basic random values with this random source, use the methods defined in the GKRandom protocol. To generate random values with a specific range and distribution, use this random source with the GKRandomDistribution class or one of its subclasses.

When you create an instance of this class, the resulting random source is both independent and deterministic—that is, the sequence of numbers generated by one instance has no effect on the sequence generated by any other instance, and that sequence can be replicated when necessary. For details on replicating sequences, see the seed property and init(seed:) initializer.

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


Creating a Random Source


Initializes a random source from a nondeterministic seed.

init(seed: Data)

Initializes a random source with the specified seed data.

Replicating Random Behavior

var seed: Data

The seed data that determines the random source’s behavior.

Avoiding Predictable Behavior

func dropValues(Int)

Skips the specified number of values in the random sequence.


Inherits From

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 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.

class GKShuffledDistribution

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