Unknown Classes Intermediate Since Laravel 10.x

The Lottery Class

Execute a callback with a given probability — perfect for sampling, feature flags, and A/B testing without external services.

Overview

Illuminate\Support\Lottery lets you run code based on odds. Think of it as a dice roll built into the framework — run something 1 in 100 requests, or 20% of the time, with a clean API.

Usage

use Illuminate\Support\Lottery;

// Run 1 out of every 100 times
Lottery::odds(1, 100)
    ->winner(fn () => logger('You won!'))
    ->loser(fn () => logger('Maybe next time.'))
    ->choose();

You can also use it inline to return values:

$result = Lottery::odds(1, 5)->winner(fn () => 'free shipping')->choose();

For testing, Lottery is fully fakeable:

Lottery::alwaysWin();

// ... your test code

Lottery::determineResultsNormally();

When to Use

  • Sampling a percentage of requests for logging or profiling
  • Gradual feature rollouts without a full feature-flag system
  • Randomly assigning users to experiments
  • Adding jitter or randomness to scheduled tasks