<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: -",
    "macOS: 12.0.0 -",
    "tvOS: 15.0.0 -",
    "visionOS: -",
    "watchOS: 8.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Accelerate",
  "identifier" : "/documentation/Accelerate/BNNS/RandomGenerator/state",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Property",
  "symbol" : {
    "kind" : "Instance Property",
    "modules" : [
      "Accelerate"
    ],
    "preciseIdentifier" : "s:10Accelerate4BNNSO15RandomGeneratorC5stateAC0cD5StateCvp"
  },
  "title" : "state"
}
-->

# state

The state of the random number generator.

```
var state: BNNS.RandomGeneratorState { get set }
```

## Discussion

Use the random number generator’s [`state`](/documentation/Accelerate/BNNS/RandomGenerator/state) property to save and restore the generator.

The following code creates a random number generator and captures its initial state. The code calls [`allocate(randomUniformUsing:range:shape:batchSize:)`](/documentation/Accelerate/BNNSNDArrayDescriptor/allocate(randomUniformUsing:range:shape:batchSize:)-761hg) twice to populate the arrays `a` and `b` with different random values. Before allocating the array `c` with random values, the code resets the generator’s state to create identical random values in arrays `a` and `c`.

```swift
guard
    let randomGenerator = BNNS.RandomGenerator(
        method: .aesCtr,
        seed: 1234) else {
        return
    }

let state = randomGenerator.state

let a = BNNSNDArrayDescriptor.allocate(
    randomUniformUsing: randomGenerator,
    range: Int16(-10)...Int16(10),
    shape: [16])!.makeArray(of: Int16.self)!

let b = BNNSNDArrayDescriptor.allocate(
    randomUniformUsing: randomGenerator,
    range: Int16(-10)...Int16(10),
    shape: [16])!.makeArray(of: Int16.self)!

// Prints "false".
print(a.elementsEqual(b))

randomGenerator.state = state

let c = BNNSNDArrayDescriptor.allocate(
    randomUniformUsing: randomGenerator,
    range: Int16(-10)...Int16(10),
    shape: [16])!.makeArray(of: Int16.self)!

// Prints "true".
print(a.elementsEqual(c))
```

---

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)