<!--
{
  "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/GKMersenneTwisterRandomSource",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "GameplayKit"
    ],
    "preciseIdentifier" : "c:objc(cs)GKMersenneTwisterRandomSource"
  },
  "title" : "GKMersenneTwisterRandomSource"
}
-->

# GKMersenneTwisterRandomSource

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

```
class GKMersenneTwisterRandomSource
```

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

To generate basic random values with this random source, use the methods defined in the [`GKRandom`](/documentation/GameplayKit/GKRandom) protocol. To generate random values with a specific range and distribution, use this random source with the [`GKRandomDistribution`](/documentation/GameplayKit/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`](/documentation/GameplayKit/GKMersenneTwisterRandomSource/seed) property and [`init(seed:)`](/documentation/GameplayKit/GKMersenneTwisterRandomSource/init(seed:)) initializer.

The Mersenne Twister random source uses a common 64-bit variant of the algorithm originally described by Matsumoto and Nishimura in 2000, based on the Mersenne prime power 19937. This random source is therefore compatible with other implementations of that algorithm, including the `mt19937_64` type from the `std::mersenne_twister_engine` template in C++11. That is, if you initialize a [`GKMersenneTwisterRandomSource`](/documentation/GameplayKit/GKMersenneTwisterRandomSource) instance and a compatible implementation using the same seed value, both generate the same sequence of numbers.

For more information on choosing and using randomizers in GameplayKit, read [Randomization](https://developer.apple.com/library/archive/documentation/General/Conceptual/GameplayKit_Guide/RandomSources.html#//apple_ref/doc/uid/TP40015172-CH9) in [GameplayKit Programming Guide](https://developer.apple.com/library/archive/documentation/General/Conceptual/GameplayKit_Guide/index.html#//apple_ref/doc/uid/TP40015172).

## Topics

### Creating a Random Source

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

Initializes a random source from a nondeterministic seed.

[`-  initWithSeed:`](/documentation/GameplayKit/GKMersenneTwisterRandomSource/init(seed:))

Initializes a random source with the specified seed value.

### Replicating Random Behavior

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

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

## Relationships

### Conforms To

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

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

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

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

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

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

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

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

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

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

### Inherits From

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

---

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)