<!--
{
  "availability" : [
    "iOS: 26.0.0 -",
    "iPadOS: 26.0.0 -",
    "macCatalyst: 26.0.0 -",
    "macOS: 26.0.0 -",
    "visionOS: 26.0.0 -",
    "watchOS: 27.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "FoundationModels",
  "identifier" : "/documentation/FoundationModels/GenerationOptions/SamplingMode-swift.struct/random(top:seed:)",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Foundation Models",
      "FoundationModels"
    ],
    "preciseIdentifier" : "s:16FoundationModels17GenerationOptionsV12SamplingModeV6random3top4seedAESi_s6UInt64VSgtFZ"
  },
  "title" : "random(top:seed:)"
}
-->

# random(top:seed:)

A sampling mode that considers a fixed number of high-probability tokens.

```
static func random(top k: Int, seed: UInt64? = nil) -> GenerationOptions.SamplingMode
```

## Parameters

`k`

The number of tokens to consider.

`seed`

An optional random seed used to make output more deterministic.

## Discussion

Also known as top-k.

During the token-selection process, the vocabulary is sorted by probability a
token is selected from among the top K candidates. Smaller values of K
ensure only the most probable tokens are candidates for selection, resulting
in more deterministic and confident answers. Larger values of K allow less
probably tokens to be selected, raising non-determinism and creativity.

> Note: Setting a random seed is not guaranteed to result in fully deterministic
> output. It is best effort.

> SeeAlso: Sampling modes ``doc://com.apple.foundationmodels/documentation/FoundationModels/GenerationOptions/SamplingMode-swift.struct/greedy`` and ``doc://com.apple.foundationmodels/documentation/FoundationModels/GenerationOptions/SamplingMode-swift.struct/random(probabilityThreshold:seed:)``

---

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)