<!--
{
  "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/RandomNumberGenerator/next(upperBound:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:SGsE4next10upperBoundqd__qd___ts17FixedWidthIntegerRd__SURd__lF"
  },
  "title" : "next(upperBound:)"
}
-->

# next(upperBound:)

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

```
mutating func next<T>(upperBound: T) -> T where T : FixedWidthInteger, T : UnsignedInteger
```

## Parameters

`upperBound`

The upper bound for the randomly generated value.
Must be non-zero.

## Return Value

A random value of `T` in the range `0..<upperBound`. Every
value in the range `0..<upperBound` is equally likely to be returned.

## Discussion

Use this method when you need random binary data to generate another
value. If you need an integer value within a specific range, use the
static `random(in:using:)` method on that integer type instead of this
method.

---

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)