<!--
{
  "availability" : [
    "iOS: 9.0.0 -",
    "iPadOS: 9.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.11.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "GameplayKit",
  "identifier" : "/documentation/GameplayKit/GKRandomSource",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "GameplayKit"
    ],
    "preciseIdentifier" : "c:objc(cs)GKRandomSource"
  },
  "title" : "GKRandomSource"
}
-->

# GKRandomSource

The superclass for all basic randomization classes in GameplayKit.

```
class GKRandomSource
```

## Overview> Important:
> The randomization services provided in GameplayKit are suitable for reliably creating deterministic, pseudorandom gameplay mechanics, but are not cryptographically robust. For cryptography, obfuscation, or cipher uses, use the Security framework, described in [Cryptographic Services Guide](https://developer.apple.com/library/archive/documentation/Security/Conceptual/cryptoservices/Introduction/Introduction.html#//apple_ref/doc/uid/TP40011172).

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`](/documentation/GameplayKit/GKRandomDistribution) classes.

When you create an instance of [`GKRandomSource`](/documentation/GameplayKit/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`](/documentation/GameplayKit/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`](/documentation/GameplayKit/GKLinearCongruentialRandomSource) class uses an algorithm that is faster, but less random, than the [`GKARC4RandomSource`](/documentation/GameplayKit/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`](/documentation/GameplayKit/GKMersenneTwisterRandomSource) class uses an algorithm that is slower, but more random, than the [`GKARC4RandomSource`](/documentation/GameplayKit/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.

## Topics

### Creating an Independent Random Source

[`-  init`](/documentation/GameplayKit/GKRandomSource/init())

Initializes a new random source object.

### Randomly Shuffling an Array

[`-  arrayByShufflingObjectsInArray:`](/documentation/GameplayKit/GKRandomSource/arrayByShufflingObjects(in:))

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`](/documentation/GameplayKit/GKRandomSource/sharedRandom())

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

### Initializers

[`-  initWithCoder:`](/documentation/GameplayKit/GKRandomSource/init(coder:))

## Relationships

### Conforms To

[`NSCopying`](/documentation/Foundation/NSCopying)

[`CVarArg`](/documentation/Swift/CVarArg)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`NSSecureCoding`](/documentation/Foundation/NSSecureCoding)

[`Equatable`](/documentation/Swift/Equatable)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`Hashable`](/documentation/Swift/Hashable)

[`NSCoding`](/documentation/Foundation/NSCoding)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`GKRandom`](/documentation/GameplayKit/GKRandom)

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

### Inherited By

[`GKLinearCongruentialRandomSource`](/documentation/GameplayKit/GKLinearCongruentialRandomSource)

[`GKMersenneTwisterRandomSource`](/documentation/GameplayKit/GKMersenneTwisterRandomSource)

[`GKARC4RandomSource`](/documentation/GameplayKit/GKARC4RandomSource)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)