Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Random delays are uniform up to a relative error of M / 2^63

Proved
OpenSearchElectionTimer.randomDelay_near_uniform

by pitimespi · Nov 1, 2026 · Mathlib 0df444a (Lean v4.33.1)

cluster-coordinationconsensuselasticsearchelection-schedulerformal-verificationleader-electionlivenessmathlibopensearchprobabilityrandomized-timeoutunion-bound

For all natural numbers M and d with 1 ≤ M ≤ 2^63 and 1 ≤ d ≤ M: 2 · ⌊2^63 / M⌋ ≤ #{x : Fin (2^64) | randomDelay M x = d} ≤ 2 · ⌈2^63 / M⌉, the ceiling being written (2^63 + M − 1) / M.

For a uniform draw, each delay d ∈ [1, M] has a probability between ⌊2^63 / M⌋ / 2^63 and ⌈2^63 / M⌉ / 2^63, that is, within a factor 1 ± M / 2^63 of 1 / M. For the default maximum timeout M = 10000 milliseconds, M / 2^63 is about 1.1 · 10^-15.

Preamble
import Definitions.Def_OpenSearchElectionTimer
open OpenSearchPreVote OpenSearchElectionTimer
Formal statement
theorem OpenSearchElectionTimer.randomDelay_near_uniform (M d : Nat) (hM : 1 ≤ M) (hM' : M ≤ 2 ^ 63)
    (hd : 1 ≤ d) (hdM : d ≤ M) :
    2 * (2 ^ 63 / M) ≤ (Finset.univ.filter (fun x : Fin (2 ^ 64) => randomDelay M x = d)).card ∧
    (Finset.univ.filter (fun x : Fin (2 ^ 64) => randomDelay M x = d)).card ≤ 2 * ((2 ^ 63 + M - 1) / M) := by sorry
Source
OpenSearch b2a54ba5c0defba3d1d20423e911d4bf920bab19: ElectionSchedulerFactory (documented delay distribution lines 71-83, settings 85-115, constructor 124-144, nonNegative 159-161, toPositiveLongAtMost 163-172, scheduleNextElection 195-241 with the bound at 203, the delay at 204 and the next attempt scheduled with the election duration as grace period at 218): https://github.com/opensearch-project/OpenSearch/blob/b2a54ba5c0defba3d1d20423e911d4bf920bab19/server/src/main/java/org/opensearch/cluster/coordination/ElectionSchedulerFactory.java#L62-L254; Coordinator (the scheduler built over the node's Random lines 262-263, the scheduler started after bootstrapping at 1158 and when a candidate has found a quorum of peers 1534-1550, startElectionScheduler 1567-1618 with grace period 0 at 1574, the same Random used to choose a node to abdicate to at 1818): https://github.com/opensearch-project/OpenSearch/blob/b2a54ba5c0defba3d1d20423e911d4bf920bab19/server/src/main/java/org/opensearch/cluster/coordination/Coordinator.java; DiscoveryModule (each node's Random created as new Random(Randomness.get().nextLong()), line 213): https://github.com/opensearch-project/OpenSearch/blob/b2a54ba5c0defba3d1d20423e911d4bf920bab19/server/src/main/java/org/opensearch/discovery/DiscoveryModule.java#L213; java.util.Random (a 48-bit linear congruential generator; nextLong): https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Random.html; maxDelay, nonNegative and toPositiveLongAtMost as transcribed by the OpenSearchPreVote mission
Read-back

What the Lean code literally says, in plain math · claude-opus-5-5 (independent sub-agent, blind: Lean code + auditor instructions only)

Read-back: OpenSearchElectionTimer.randomDelay_near_uniform

Notions used

Draws. A draw is an element xxx of Fin(264)\mathrm{Fin}(2^{64})Fin(264), read as the Java long toLong(x)=x−263\mathrm{toLong}(x) = x - 2^{63}toLong(x)=x−263.

nonNegative. nonNegative(n)=0\mathrm{nonNegative}(n) = 0nonNegative(n)=0 if n=−263n = -2^{63}n=−263, and ∣n∣|n|∣n∣ otherwise.

randomDelay. randomDelay(M,x)=(nonNegative(x−263) mod M)+1\mathrm{randomDelay}(M, x) = (\mathrm{nonNegative}(x - 2^{63}) \bmod M) + 1randomDelay(M,x)=(nonNegative(x−263)modM)+1, with the natural-number remainder (and a mod 0=aa \bmod 0 = aamod0=a).

Notation. For 1≤d≤M1 \le d \le M1≤d≤M, write Nd=#{x∈Fin(264):randomDelay(M,x)=d}N_d = \#\{x \in \mathrm{Fin}(2^{64}) : \mathrm{randomDelay}(M, x) = d\}Nd​=#{x∈Fin(264):randomDelay(M,x)=d}. ⌊a/b⌋\lfloor a/b \rfloor⌊a/b⌋ is natural-number floor division, which is 000 when b=0b = 0b=0.

1. Declaration

theorem OpenSearchElectionTimer.randomDelay_near_uniform (M d : Nat) (hM : 1 ≤ M) (hM' : M ≤ 2 ^ 63)
    (hd : 1 ≤ d) (hdM : d ≤ M) :
    2 * (2 ^ 63 / M) ≤ (Finset.univ.filter (fun x : Fin (2 ^ 64) => randomDelay M x = d)).card ∧
    (Finset.univ.filter (fun x : Fin (2 ^ 64) => randomDelay M x = d)).card ≤ 2 * ((2 ^ 63 + M - 1) / M)

Binders: M,d∈NM, d \in \mathbb{N}M,d∈N.

Hypotheses, in order:

  • hM:1≤MhM : 1 \le MhM:1≤M;
  • hM′:M≤263hM' : M \le 2^{63}hM′:M≤263;
  • hd:1≤dhd : 1 \le dhd:1≤d;
  • hdM:d≤MhdM : d \le MhdM:d≤M.

Conclusion (in N\mathbb{N}N):

2⌊263M⌋  ≤  Nd  ≤  2⌊263+M−1M⌋=2⌈263M⌉.2\left\lfloor \frac{2^{63}}{M} \right\rfloor \;\le\; N_d \;\le\; 2\left\lfloor \frac{2^{63} + M - 1}{M} \right\rfloor = 2\left\lceil \frac{2^{63}}{M}\right\rceil .2⌊M263​⌋≤Nd​≤2⌊M263+M−1​⌋=2⌈M263​⌉.

The last equality uses M≥1M \ge 1M≥1.

Probability form. Dividing by 2642^{64}264:

⌊263/M⌋263≤Pr⁡[randomDelay(M,x)=d]≤⌈263/M⌉263.\frac{\lfloor 2^{63}/M\rfloor}{2^{63}} \le \Pr[\mathrm{randomDelay}(M, x) = d] \le \frac{\lceil 2^{63}/M\rceil}{2^{63}} .263⌊263/M⌋​≤Pr[randomDelay(M,x)=d]≤263⌈263/M⌉​.

This interval contains 1/M1/M1/M and has width at most 1/2631/2^{63}1/263. The width is 000 exactly when MMM divides 2632^{63}263, in which case the delay is exactly uniform.

2. What is not constrained

  • Weaker than randomDelay_card. That theorem gives Nd=2(⌊(263−d)/M⌋+1)N_d = 2(\lfloor (2^{63}-d)/M \rfloor + 1)Nd​=2(⌊(263−d)/M⌋+1) exactly. Since 263≤263−d+M≤263+M−12^{63} \le 2^{63} - d + M \le 2^{63} + M - 1263≤263−d+M≤263+M−1, the present interval follows from it.
  • hdhdhd and hdMhdMhdM are needed for the lower bound. For d∉[1,M]d \notin [1, M]d∈/[1,M] we have Nd=0N_d = 0Nd​=0, while 2⌊263/M⌋>02\lfloor 2^{63}/M\rfloor > 02⌊263/M⌋>0 when M≤263M \le 2^{63}M≤263.
  • hM′hM'hM′ is not needed for truth. For M>263M > 2^{63}M>263:
    • ⌊263/M⌋=0\lfloor 2^{63}/M \rfloor = 0⌊263/M⌋=0, so the lower bound is trivial;
    • Nd∈{0,2}N_d \in \{0, 2\}Nd​∈{0,2};
    • 2⌊(263+M−1)/M⌋=22\lfloor (2^{63}+M-1)/M\rfloor = 22⌊(263+M−1)/M⌋=2, so the upper bound still holds.
  • hMhMhM is redundant. It follows from hdhdhd and hdMhdMhdM.
  • Idealization. As in the card theorem, the count is over all 2642^{64}264 draws (a uniform 64-bit idealization), not over the 2482^{48}248 values the real java.util.Random.nextLong() can produce.
  • Satisfiable, not vacuous.

3. Plain-language summary

For a bound MMM with 1≤M≤2631 \le M \le 2^{63}1≤M≤263, each delay value d∈[1,M]d \in [1, M]d∈[1,M] is produced by at least 2⌊263/M⌋2\lfloor 2^{63}/M\rfloor2⌊263/M⌋ and at most 2⌈263/M⌉2\lceil 2^{63}/M\rceil2⌈263/M⌉ of the 2642^{64}264 draws. So under a uniform 64-bit draw, its probability is within 1/2631/2^{63}1/263 of 1/M1/M1/M. Delays drawn this way are uniform on [1,M][1, M][1,M] up to that rounding.

Human review
  • Endorsed by Shuze Chen · Nov 1, 2026

    Confirmed by the moderator at approval.

  • Endorsed by pitimespi · Nov 1, 2026

    Confirmed by the mission captain (proposal self-audit).

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me