<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/SystemRandomNumberGenerator",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:s27SystemRandomNumberGeneratorV"
  },
  "title" : "SystemRandomNumberGenerator"
}
-->

# SystemRandomNumberGenerator

The system’s default source of random data.

```
@frozen struct SystemRandomNumberGenerator
```

## Overview

When you generate random values, shuffle a collection, or perform another
operation that depends on random data, this type is the generator used by
default. For example, the two method calls in this example are equivalent:

```
let x = Int.random(in: 1...100)
var g = SystemRandomNumberGenerator()
let y = Int.random(in: 1...100, using: &g)
```

`SystemRandomNumberGenerator` is automatically seeded, is safe to use in
multiple threads, and uses a cryptographically secure algorithm whenever
possible.

# Platform Implementation of `SystemRandomNumberGenerator`

While the system generator is automatically seeded and thread-safe on every
platform, the cryptographic quality of the stream of random data produced by
the generator may vary. For more detail, see the documentation for the APIs
used by each platform.

- Apple platforms use `arc4random_buf(3)`.
- Linux platforms use `getrandom(2)` when available; otherwise, they read
  from `/dev/urandom`.
- Windows uses `BCryptGenRandom`.

## Topics

### Creating a Generator

[`init()`](/documentation/Swift/SystemRandomNumberGenerator/init())

Creates a new instance of the system’s default random number generator.

### Generating Random Binary Data

[`next()`](/documentation/Swift/SystemRandomNumberGenerator/next())

Returns a value from a uniform, independent distribution of binary data.

[`next()`](/documentation/Swift/SystemRandomNumberGenerator/next()-2x0ly)

Returns a value from a uniform, independent distribution of binary data.

[`next(upperBound:)`](/documentation/Swift/SystemRandomNumberGenerator/next(upperBound:))

Returns a random value that is less than the given upper bound.



---

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)
