Rejection sampling
Rejection sampling is a method that generates candidate samples and accepts those meeting a specified criterion while discarding the others.
Fabulous Adventures in Data Structures and Algorithms
The rejection algorithm samples candidate outcomes and discards candidates that fail a probabilistic acceptance test.
- How it is used
- Uses easy-to-sample helper distributions to generate categorical or continuous samples that follow desired weights or densities.
- How deep it goes
- Works through dartboard intuition, acceptance code, helper scaling, clamping, and unbounded retry costs.
- Examples
- Demonstrates weighted integer categories, a two-humped continuous density, and a normal distribution clamped to [0.0, 1.0].
- What you can do after
- Build it from scratch in C# using uniform samplers, a helper distribution, a target weight function, and an acceptance loop.
Read how Fabulous Adventures in Data Structures and Algorithms explains rejection sampling →
Reinforcement Learning from Human Feedback
Rejection sampling is a sampling method that accepts or filters candidate outputs according to a specified criterion or scoring procedure.
- How it is used
- Deploys reward-model filtering as a simple alternative to reinforcement learning for preference fine-tuning.
- How deep it goes
- Explains four pipeline stages and selection strategies, but defers prompt, reward-model, sequencing, and deduplication choices to experimentation.
- Examples
- Uses WebGPT, Helpful and Harmless, process reward models, Llama 2 Chat, and DeepSeek R1 as pipeline examples.
- What you can do after
- Operate/configure a generate-rank-fine-tune pipeline and choose between per-prompt and global completion selection.
Read how Reinforcement Learning from Human Feedback explains rejection sampling →
Building LLM Applications with DSPy
Rejection sampling is a method that accepts candidate samples meeting a criterion and rejects those that do not.
- How it is used
- Uses rejection sampling during DSPy bootstrapping to select safe training examples as demonstrations.
- How deep it goes
- Describes metric-based acceptance, training-set order, and repeated rounds without formalizing the sampling method.
- Examples
- Uses exact-match intent labels to accept correct predictions and reject incorrect ones.
- What you can do after
- Apply rejection sampling with DSPy's BootstrapFewShot to compile a program using accepted training examples as demonstrations.
Read how Building LLM Applications with DSPy explains rejection sampling →
Related concepts
- reward modeluses · A reward model is a learned model that assigns scores to outputs, actions, or outcomes to represent their desirability according to preferences or evaluation criteria.
- reinforcement learningalternative to · Reinforcement learning is a machine-learning paradigm in which an agent learns actions or a policy through interaction with an environment and evaluative feedback, such as rewards.
- pipelinepart of · A pipeline is an ordered or coordinated sequence of connected processing stages or tasks that passes data, work, or results toward an outcome.
- data curationkind of · Data curation is the systematic selection, review, cleaning, organization, and maintenance of data for a particular purpose.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.


