<!--
{
  "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/Int/random(in:using:)-4lsb5",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:s17FixedWidthIntegerPsE6random2in5usingxSnyxG_qd__ztSGRd__lFZ::SYNTHESIZED::s:Si"
  },
  "title" : "random(in:using:)"
}
-->

# random(in:using:)

Returns a random value within the specified range, using the given
generator as a source for randomness.

```
static func random<T>(in range: Range<Self>, using generator: inout T) -> Self where T : RandomNumberGenerator
```

## Parameters

`range`

The range in which to create a random value.
`range` must not be empty.

`generator`

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

## Return Value

A random value within the bounds of `range`.

## Discussion

Use this method to generate an integer within a specific range when you
are using a custom random number generator. This example creates three
new values in the range `1..<100`.

```
for _ in 1...3 {
    print(Int.random(in: 1..<100, using: &myGenerator))
}
// Prints "7"
// Prints "44"
// Prints "21"
```

> 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 integer 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)