The superclass for all basic randomization classes in GameplayKit.


@interface GKRandomSource : NSObject


In most cases you do not use this class directly; instead, use one of the subclasses listed below. On its own, a random source provides little control over the range or distribution of generated random numbers; to specify such options, use a random source in conjunction with one of the GKRandomDistribution classes.

When you create an instance of GKRandomSource or one of its subclasses, 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 another instance, and that sequence can be replicated when necessary. For details on replicating sequences, see each of the random source classes.

Your choice of random source class determines the algorithm used for random number generation:

  • The GKARC4RandomSource class uses an algorithm similar to that employed in arc4random family of C functions. (However, instances of this class are independent from calls to the arc4random functions.)

  • The GKLinearCongruentialRandomSource class uses an algorithm that is faster, but less random, than the GKARC4RandomSource class. (Specifically, the low bits of generated numbers repeat more often than the high bits.) Use this source when performance is more important than robust unpredictability.

  • The GKMersenneTwisterRandomSource class uses an algorithm that is slower, but more random, than the GKARC4RandomSource class. Use this source when it’s important that your use of random numbers not show repeating patterns and performance is of less concern.


Creating an Independent Random Source

- init

Initializes a new random source object.

Randomly Shuffling an Array

- arrayByShufflingObjectsInArray:

Returns an array whose contents are the same as those of the specified array, but in a random order determined by the random source.

Using a Shared Random Source

+ sharedRandom

Returns a shared instance that shares a system-wide random source.



Inherits From

See Also



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


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


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


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


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


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


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