The number of rejection attempts randomIndex makes before it returns
index zero.
Equations
Instances For
Read k generator draws as the base-width digits of a number below
width ^ k, least significant digit first. Each draw is shifted down by lo
and reduced modulo width. The reduction changes nothing for a draw in the
generator's range [lo, lo + width - 1].
Equations
- Hex.PermGroup.randomDigits lo width 0 = pure 0
- Hex.PermGroup.randomDigits lo width k.succ = do let d ← Rand.next let rest ← Hex.PermGroup.randomDigits lo width k pure ((d - lo) % width + width * rest)
Instances For
Starting from (k, span), multiply span by width at most fuel times
until it reaches bound, and return the final count and span.
Equations
Instances For
An index in Fin bound drawn by rejection sampling from the generator.
Let [lo, hi] be the generator's range and width = hi + 1 - lo. Take the
least k with bound ≤ width ^ k and span = width ^ k. One attempt reads k
draws as a number x < span (randomDigits) and accepts it when x is below
limit, the largest multiple of bound that is at most span, returning
x % bound. No modular reduction is applied to an incomplete range.
If the draws are independent and uniform on [lo, hi], then x is uniform
below span, an accepted x is uniform below limit, and x % bound is
exactly uniform on Fin bound. Since bound ≤ span, limit > span / 2, so
each attempt accepts with probability above one half. After
randomIndexAttempts = 128 rejections in a row the result is index zero.
That happens with probability below 2 ^ (-128), so each index has
probability within 2 ^ (-128) of 1 / bound, and exactly 1 / bound
conditional on some attempt being accepted. The fixed number of attempts keeps
the definition total in RandGT g m for every monad m, including for
generators that are not uniform. A range with fewer than two values carries
no randomness, and the result is then index zero without any draw.
Equations
- One or more equations did not get rendered due to their size.
Instances For
At most fuel attempts with acceptance threshold limit.
Equations
- One or more equations did not get rendered due to their size.
- Hex.PermGroup.randomIndex.attempt bound h lo width k limit 0 = pure ⟨0, h⟩
Instances For
The same indexing identifies the corresponding Mathlib subgroup.
Equations
Instances For
Conditional uniformity: unranking preserves uniform mass supplied on the bounded indices. No assumption about a pseudorandom implementation is made.
An element of G drawn with Mathlib's random-generator interface: unrank an
index from randomIndex. If the generator's draws are independent and uniform
on its range, each element has probability within 2 ^ (-128) of
1 / order G, and exactly 1 / order G conditional on the index draw
accepting within its attempt limit. Mathlib's randFin is not used, because it
reduces a draw modulo the bound and is therefore biased.
Instances For
Enumerated permutation values are exactly the corresponding subgroup.