<!--
{
  "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/Bool/random(using:)",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:Sb6random5usingSbxz_tSGRzlFZ"
  },
  "title" : "random(using:)"
}
-->

# random(using:)

Returns a random Boolean value, using the given generator as a source for
randomness.

```
static func random<T>(using generator: inout T) -> Bool where T : RandomNumberGenerator
```

## Parameters

`generator`

The random number generator to use when creating
the new random value.

## Return Value

Either `true` or `false`, randomly chosen with equal
probability.

## Discussion

This method returns `true` and `false` with equal probability. Use this
method to generate a random Boolean value when you are using a custom
random number generator.

```
let flippedHeads = Bool.random(using: &myGenerator)
if flippedHeads {
    print("Heads, you win!")
} else {
    print("Maybe another try?")
}
```

> Note: The algorithm used to create random values may change in a future
> version of Swift. If you’re passing a generator that results in the
> same sequence of Boolean values each time you run your program, that
> sequence may change when your program is compiled using a different
> version of Swift.

---

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)