<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2327: Non-power-of-two URNGs should be forbidden</title>
<meta property="og:title" content="Issue 2327: Non-power-of-two URNGs should be forbidden">
<meta property="og:description" content="C++ library issue. Status: NAD">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2327.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#NAD">NAD</a> status.</em></p>
<h3 id="2327"><a href="lwg-closed.html#2327">2327</a>. Non-power-of-two URNGs should be forbidden</h3>
<p><b>Section:</b> 29.5.3.3 <a href="https://wg21.link/rand.req.urng">[rand.req.urng]</a> <b>Status:</b> <a href="lwg-active.html#NAD">NAD</a>
 <b>Submitter:</b> Stephan T. Lavavej <b>Opened:</b> 2013-09-21 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#rand.req.urng">issues</a> in [rand.req.urng].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#NAD">NAD</a> status.</p>
<p><b>Discussion:</b></p>
<p>
29.5.3.3 <a href="https://wg21.link/rand.req.urng">[rand.req.urng]</a> allows URNGs with non-power-of-two (NPOT) ranges, like <code>[0, 1729]</code>. This is unnecessarily permissive 
(I cannot imagine a realistic source of randomness that would generate such a range) and has real costs for implementers, as 
<code>uniform_int_distribution</code> must be prepared to accept such URNGs. The most efficient way to accumulate randomness is to 
concatenate random bits, so NPOT randomness is not just useless, it is actively harmful (to avoid bias, if a URNG generates a 
random number outside of a power-of-two range, the number must be discarded).
<p/>
Forbidding NPOT URNGs wouldn't affect users, and would simplify Standard Library implementations. It would be nice to require 
<code>min()</code> to be <code>0</code>, but this is not necessary; it is simple for implementations to say <code>g() - G::min()</code> and 
this will optimize away if <code>min()</code> is <code>0</code>. (It is vaguely plausible for a URNG to have a nonzero minimum; I can 
imagine something that simply masks off low-order bits without shifting the rest downwards.) What is important is for the entire 
range to have a power-of-two width; <code>[1729, 1984]</code> is acceptable as its size is 256.
</p>

<p><i>[2013-10-12: Howard presents a counterexample]</i></p>


<p>
Consider:
</p>
<blockquote><pre>
#include &lt;random&gt;
#include &lt;string&gt;
#include &lt;iostream&gt;

template &lt;class Int&gt;
bool is_power_2m1(Int i)
{
  return (i &amp; (i + 1)) == 0;
}

template &lt;class URNG&gt;
void test(const std::string&amp; urng)
{
  using namespace std;
  typename URNG::result_type rng = URNG::max() - URNG::min();
  if (!is_power_2m1(rng))
  {
    cout &lt;&lt; hex;
    cout &lt;&lt; urng &lt;&lt; " : min = " &lt;&lt; URNG::min() &lt;&lt; ", max = " &lt;&lt; URNG::max()
         &lt;&lt; ", max-min = " &lt;&lt; rng &lt;&lt; '\n';
  }
};

int main()
{
    using namespace std;
    test&lt;minstd_rand0&gt;("minstd_rand0");
    test&lt;minstd_rand&gt;("minstd_rand");
    test&lt;mt19937&gt;("mt19937");
    test&lt;mt19937_64&gt;("mt19937_64");
    test&lt;ranlux24_base&gt;("ranlux24_base");
    test&lt;ranlux48_base&gt;("ranlux48_base");
    test&lt;ranlux24&gt;("ranlux24");
    test&lt;ranlux48&gt;("ranlux48");
    test&lt;knuth_b&gt;("knuth_b");
}
</pre></blockquote>

<p>
Which for me outputs:
</p>

<blockquote><pre>
minstd_rand0 : min = 1, max = 7ffffffe, max-min = 7ffffffd
minstd_rand : min = 1, max = 7ffffffe, max-min = 7ffffffd
knuth_b : min = 1, max = 7ffffffe, max-min = 7ffffffd
</pre></blockquote>

<p>
We do not want to outlaw these three URNG's, and the proposed wording would do that.
</p>

<p><i>[Issaquah 2014-02-10: Moved to NAD]</i></p>

<p>
STL withdraws the issue, non-power-of-2 URNGs are used in the field, it is too late to consider removing them.
</p>



<p id="res-2327"><b>Proposed resolution:</b></p>
<p>This wording is relative to N3691.</p>

<ol>
<li><p>Add a new paragraph at the end of 29.5.3.3 <a href="https://wg21.link/rand.req.urng">[rand.req.urng]</a> as indicated:</p>

<blockquote>
<p>
-3- The following relation shall hold: <code>G::min() &lt; G::max()</code>.
<p/>
<ins>-?- <code>G::max() - G::min()</code> shall be 2<sup><code>n</code></sup> - 1 for some <code>n &gt; 0</code>.</ins>
</p>
</blockquote>
</li>
</ol>





</body>
</html>
