Skip to main content
QUICK REVIEW

[Paper Review] Optimal Discrete Uniform Generation from Coin Flips, and Applications

Jérémie Lumbroso|arXiv (Cornell University)|Apr 6, 2013
Algorithms and Data CompressionComputer Science15 references14 citations
TL;DR

This paper presents an optimal algorithm for generating discrete uniform random variables from random bits, minimizing both bit usage and computational cost. By leveraging a bitstream-based approach inspired by Knuth and Yao’s framework, it achieves information-theoretic optimality in random bit consumption while enabling efficient Monte Carlo simulations in physics and biology.

ABSTRACT

This article introduces an algorithm to draw random discrete uniform variables within a given range of size n from a source of random bits. The algorithm aims to be simple to implement and optimal both with regards to the amount of random bits consumed, and from a computational perspective---allowing for faster and more efficient Monte-Carlo simulations in computational physics and biology. I also provide a detailed analysis of the number of bits that are spent per variate, and offer some extensions and applications, in particular to the optimal random generation of permutations.

Motivation & Objective

  • Address the inefficiency of existing methods that waste random bits when sampling discrete uniform distributions.
  • Develop a practical algorithm that consumes the minimal number of random bits required for any range size n.
  • Ensure computational efficiency by avoiding costly divisions and modulo operations common in standard implementations.
  • Enable information-theoretically optimal sampling for use in high-performance simulations requiring billions of variates.
  • Extend the approach to optimal generation of random permutations via unranking using Fisher-Yates shuffle with optimal bit usage.

Proposed method

  • Use a bitstream-based consumption model where each random bit is drawn sequentially from a source of unbiased random bits.
  • Implement a recursive interval search method akin to the interval algorithm of Han and Hoshi, maintaining a current interval that brackets the target probability.
  • Maintain a running interval [a, b) that is repeatedly halved based on bit outcomes, narrowing down to a subinterval containing the desired uniform variate.
  • Terminate when the interval is small enough to determine a unique integer in [0, n), ensuring exact uniformity.
  • Use bit-level operations and buffering of 32-bit integers to minimize overhead while maintaining optimal bit usage.
  • Apply the same framework to generate permutations via Lehmer codes, using the generated uniform variates as inputs to the Fisher-Yates shuffle.

Experimental results

Research questions

  • RQ1What is the minimal number of random bits required to generate a discrete uniform variate over a range of size n?
  • RQ2Can an algorithm be designed that achieves this theoretical minimum while remaining computationally efficient?
  • RQ3How does bitstream-based sampling compare to standard methods using 32-bit integers in terms of overall efficiency and performance?
  • RQ4Can this optimal sampling framework be extended to generate random permutations with information-theoretic optimality?
  • RQ5What is the bit complexity of sampling without replacement, and can it be improved using this approach?

Key findings

  • The proposed algorithm achieves information-theoretic optimality in random bit usage, consuming exactly the minimum expected number of bits required for any n.
  • The algorithm avoids costly divisions and modulo operations by using bit-level manipulation and buffering of 32-bit words.
  • For small n, the algorithm significantly reduces rejection rate compared to truncation-based methods, especially when n is not a power of two.
  • The method enables optimal generation of random permutations by using the uniform variates as inputs to the Fisher-Yates shuffle, achieving both bit and time efficiency.
  • The algorithm’s performance is robust across different word sizes, supporting permutations up to n=12 (32-bit), n=21 (64-bit), and n=33 (128-bit) registers.
  • The framework provides a foundation for analyzing the bit complexity of other sampling methods, such as the alias method and sampling without replacement.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.