One Logo Pool Ball
FoundationModels · IOS API REFERENCE

GenerationOptions.SamplingMode

struct · iOS 26.0+ · SDK 27.1

struct SamplingMode

A type that defines how values are sampled from a probability distribution. A model builds its response to a prompt in a loop. At each iteration in the loop the model produces a probability distribution for all the tokens in its vocabulary. The sampling mode controls how a token is selected from that distribution.

Members (4)

get · iOS 27.0+

kind

TypeScript
get kind(): GenerationOptions_SamplingMode_Kind
Swift declaration
let kind: GenerationOptions.SamplingMode.Kind
get · iOS 26.0+

greedy

TypeScript
static get greedy(): GenerationOptions_SamplingMode
Swift declaration
static var greedy: GenerationOptions.SamplingMode { get }

A sampling mode that always chooses the most likely token. This mode always produces the same output for a given input. Responses produced with greedy sampling are statistically likely, but may lack the human-like quality and variety of other sampling strategies. - SeeAlso: Sampling modes ``random(top:seed:)`` and ``random(probabilityThreshold:seed:)``

static · iOS 26.0+

random

TypeScript
static randomProbabilityThresholdSeed(probabilityThreshold: number, seed?: (number) | null): GenerationOptions_SamplingMode
Swift declaration
static func random(probabilityThreshold: Double, seed: UInt64? = nil) -> GenerationOptions.SamplingMode

A mode that considers a variable number of high-probability tokens based on the specified threshold. Also known as top-p or nucleus sampling. With nucleus sampling, tokens are sorted by probability and added to a pool of candidates until the cumulative probability of the pool exceeds the specified threshold, and then a token is sampled from the pool. Because the number of tokens isn't predetermined, the selection pool size is larger when the distribution is flat and smaller when it is spikey. This variability can lead to a wider variety of options to choose from, and potentially more creative responses. - Note: Setting a random seed is not guaranteed to result in fully deterministic output. It is best effort. - Parameters: - probabilityThreshold: A number between `0.0` and `1.0` that increases sampling pool size. - seed: An optional random seed used to make output more deterministic. - SeeAlso: Sampling modes ``greedy`` and ``random(top:seed:)``

static · iOS 26.0+

random

TypeScript
static randomTopSeed(k: number, seed?: (number) | null): GenerationOptions_SamplingMode
Swift declaration
static func random(top k: Int, seed: UInt64? = nil) -> GenerationOptions.SamplingMode

A sampling mode that considers a fixed number of high-probability tokens. 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. - Parameters: - k: The number of tokens to consider. - seed: An optional random seed used to make output more deterministic. - SeeAlso: Sampling modes ``greedy`` and ``random(probabilityThreshold:seed:)``

Skipped declarations

No declarations were skipped in this snapshot.

All FoundationModels types →