title: Frugal conversion of uniformly distributed random numbers from one range to another
body: <p>Is there a way to convert uniformly distributed random numbers of one range to uniformly distributed random numbers of another range <strong>frugally</strong>?</p>
  <p>Let me explain what I mean by &quot;<strong>frugally</strong>&quot;.</p>
  <p>The typical approach to generate random number within given range (e.g. r ∈ [0..10) ) is to take some fixed random bits, let's say 31, which result non-negative random number less than 2147483648. Then make sure that the value is less than 2147483640 (because 2147483648 is not divisible by 10, and hence may lead to uneven distribution). If the value is greater or equal to 2147483640, throw it away and try again (get next 31 random bits and so on). If value if less than 2147483640, then just return the remainder of division by 10. This approach consumes at least 31 bit per decimal digit. Since theoretical limit is log<sub>2</sub>(10) = 3.321928..., it is quite wasteful.</p>
  <p>We can improve this, if we use 4 bits instead if 31. In this case we will consume 4 × 1.6 = 6.4 bits per decimal digit. This is more <strong>frugal</strong>, but still far from the ideal.</p>
  <pre class="lang-java prettyprint-override"><code>    public int nextDit() {
          int result;
          do {
              result = next4Bits();
          } while (result &gt;= 10);
          return result;
      }
  </code></pre>
  <p>We can try to generate 3 decimal digits at once. Since 1024 is quite close to 1000, the probability that raw source random number will be rejected is less than in previous case. Once we generated 3 decimal digits, we return 1 digit and reserve the rest 2 digits.</p>
  <p>Something like below</p>
  <pre class="lang-java prettyprint-override"><code>    private int _decDigits = 0;
      private int _decCount = 0;
  
      public int nextDit() {
          if (_decCount &gt; 0) {
              // take numbers from the reserve
              int result = _decDigits % 10;
              _decDigits /= 10;
              _decCount -= 1;
              return result;
          } else {
              int result;
              do {
                  result = next10Bits();
              } while (result &gt;= 1000);
              // reserve 2 decimal digits
              _decCount = 2;
              _decDigits = result % 100;
              result /= 100;
              return result;
          }
      }
  </code></pre>
  <p>This approach is much more <strong>frugal</strong>: it consumes 10 × 1.024 / 3 = 3.41(3) bits per decimal digit.</p>
  <p>We can even go farther if we try to reuse the numbers, which we previously have been throwing away. The random number r ∈ [0, 1024) falls into one of the 3 ranges: [0, 1000), [1000, 1020), [1020, 1024).</p>
  <p>If it falls into [0, 1000), we do as we did before, reserve 2 decimal digits (in decimal digit reserve) and return 1 decimal digit.</p>
  <p>If it falls into [1000, 1020), we subtract 1000 converting to the range [0, 20). Then we get 1 bit by dividing it by 10 and 1 decimal digit by getting remainder of division by 10. We put the bit to the binary digit reserve and return the decimal digit.</p>
  <p>If it falls into [1020, 1024), we subtract 1020 converting to the range [0, 4). Here we get just 2 bits, which we put to the binary digits reserve.</p>
  <pre class="lang-java prettyprint-override"><code>    // decimal digit reserve
      private int _decDigits = 0;
      private int _decCount = 0;
      // binary digit reserve
      private int _binDigits = 0;
      private int _binCount = 0;
  
      private int nextBits(int bits, int n) {
          for (int i = 0; i &lt; n; i += 1) {
              bits = (bits &lt;&lt; 1) + _bitRandomDevice.nextBit();
          }
          return bits;
      }
  
      private int next10Bits() {
          // take bits from the binary reserve first, then from _bitRandomDevice
          int result;
          if (_binCount &gt;= 10) {
              result = _binDigits &gt;&gt; (_binCount - 10);
              _binDigits = _binDigits &amp; (1 &lt;&lt; (_binCount - 10) - 1);
              _binCount -= 10;
          } else {
              result = nextBits(_binDigits, 10 - _binCount);
              _binCount = 0;
              _binDigits = 0;
          }
          return result;
      }
  
      public int nextDit() {
          if (_decCount &gt; 0) {
              // take numbers from the decimal reserve
              int result = _decDigits % 10;
              _decDigits /= 10;
              _decCount -= 1;
              return result;
          } else {
              int result;
              while (true) {
                  result = next10Bits();
                  if (result &lt; 1000) {
                      assert result &gt;= 0 &amp;&amp; result &lt; 1000;
                      // reserve 2 decimal digits
                      _decCount = 2;
                      _decDigits = result % 100;
                      result /= 100;
                      // return 1 decimal digit
                      return result;
                  } else if (result &lt; 1020) {
                      result -= 1000;
                      assert result &gt;= 0 &amp;&amp; result &lt; 20;
                      // reserve 1 binary digit
                      _binCount += 1;
                      _binDigits = (_binDigits &lt;&lt; 1) + (result / 10);
                      // return 1 decimal digit
                      return result % 10;
                  } else {
                      result -= 1020;
                      assert result &gt;= 0 &amp;&amp; result &lt; 4;
                      // reserve 2 binary digits
                      _binCount += 2;
                      _binDigits = (_binDigits &lt;&lt; 2) + result;
                  }
              }
          }
      }
  </code></pre>
  <p>This approach consumes about 3.38... bits per decimal digit. This is the most <strong>frugal</strong> approach I can find, but it still wastes/loses some information from the source of randomness.</p>
  <p>Thus, my question is: Is there any universal approach/algorithm that converts uniformly distributed random numbers of one arbitrary range [0, s) (later called source numbers) to uniformly distributed random numbers of another arbitrary range [0, t) (later called target numbers), consuming only log<sub>s</sub>(t) + C source numbers per target number? where C is some constant.
  If there is no such approach, why? What prevents from reaching the ideal limit?</p>
  <p>The purpose of being frugal is to reduce number of calls to RNG. This could be especially worth to do when we work with True RNG, which often has limited throughput.</p>
  <p>As for &quot;frugality optimizations&quot;, they are based on following assumptions:</p>
  <ul>
  <li>given uniform random number r ∈ [0,N), after checking that <em>r</em> &lt; M (if M &lt;= N), we may assume that it's uniformly distributed in [0,M). Traditional rejection approach is actually based on this assumption. Similarly, after checking that <em>r</em> &gt;= <em>M</em>, we may assume that it's uniformly distributed in [M,N).</li>
  <li>given uniform random number r ∈ [A,B), the derived random number (r+C) is uniformly distributed in [A+C,B+C). I.e. we can add and subtract any constant to random number to shift its range.</li>
  <li>given uniform random number r ∈ [0,N), where N=P × Q, the derived random numbers (r%P) is uniformly distributed in [0,P) and (r/P) is uniformly distributed in [0,Q). I.e. we can split one uniform random number into several ones.</li>
  <li>given uniform random numbers p ∈ [0,P) and q ∈ [0,Q), the derived random number (q× P + p) is uniformly distributed in [0,P × Q). I.e. we can combine uniform random numbers into one.</li>
  </ul>