<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 799: Mersenne twister equality overspecified</title>
<meta property="og:title" content="Issue 799: Mersenne twister equality overspecified">
<meta property="og:description" content="C++ library issue. Status: NAD">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue799.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="799"><a href="lwg-closed.html#799">799</a>. Mersenne twister equality overspecified</h3>
<p><b>Section:</b> 29.5.4.3 <a href="https://wg21.link/rand.eng.mers">[rand.eng.mers]</a>, 99 [tr.rand.eng.mers] <b>Status:</b> <a href="lwg-active.html#NAD">NAD</a>
 <b>Submitter:</b> Stephan Tolksdorf <b>Opened:</b> 2008-02-18 <b>Last modified:</b> 2023-05-24</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#rand.eng.mers">issues</a> in [rand.eng.mers].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#NAD">NAD</a> status.</p>
<p><b>Discussion:</b></p>
<p>
99 [tr.rand.eng.mers](10) requires that <code>operator==</code> for the <code>mersenne_twister</code>
returns <code>true</code> if and only if the states of two <code>mersenne_twisters</code>,
consisting each of <code>n</code> integers between <code>0</code> and <code>2<sup>w</sup> - 1</code>, are completely
equal. This is a contradiction with 99 [tr.rand.req](3) because the given
definition of the state also includes the lower <code>r</code> bits of <code>x(i-n)</code>, which
will never be used to generate a random number. If two <code>mersenne_twister</code>s
only differ in the lower bits of <code>x(i-n)</code> they will not compare equal,
although they will produce an identical sequence of random numbers.
</p>

<p>
29.5.4.3 <a href="https://wg21.link/rand.eng.mers">[rand.eng.mers]</a> in the latest C++ draft does not specify the behaviour
of <code>operator==</code> but uses a similar definition of the state and, just like
99 [tr.rand.eng.mers], requires the textual representation of a
<code>mersenne_twister_engine</code> to consist of <code>X<sub>i-n</sub></code> to <code>X<sub>i-1</sub></code>, including the
lower bits of <code>X<sub>i-n</sub></code>. This leads to two problems: First, the
unsuspecting implementer is likely to erroneously compare the lower <code>r</code>
bits of <code>X<sub>i-n</sub></code> in <code>operator==</code>. Second, if only the lower <code>r</code> bits differ,
two <code>mersenne_twister_engine</code>s will compare equal (if correctly
implemented) but have different textual representations, which
conceptually is a bit ugly.
</p>

<p>
I propose that a paragraph or footnote is added to 29.5.4.3 <a href="https://wg21.link/rand.eng.mers">[rand.eng.mers]</a> which
clarifies that the lower <code>r</code> bits of <code>X<sub>i-n</sub></code> are not to be compared in
<code>operator==</code> and <code>operator!=</code>. It would only be consequent if furthermore
the specification for the textual respresentation was changed to
<code>X<sub>i-n</sub> bitand ((2<sup>w</sup> - 1) - (2<sup>r</sup> - 1)), X<sub>i-(n-1)</sub>, ...,  X<sub>i-1</sub></code> or
something similar.
</p>

<p>
These changes would likely have no practical effect, but would allow an
implementation that does the right thing to be standard-conformant.
</p>

<p><i>[
Bellevue:
]</i></p>


<blockquote>
<p>
Fermi Lab has no objection to the proposed change. However it feels that
more time is needed to check the details, which would suggest a change
to REVIEW.
</p>
<p>
Bill feels that this is NAD, not enough practical importance to abandon
the simple definition of equality, and someone would have to do a lot
more study to ensure that all cases are covered for a very small
payback. The submitter admits that "These changes would likely have no
practical effect,", and according to Plum's razor this means that it is
not worth the effort!
</p>
<p>
Revisted: Agree that the fact that there is no practical difference means that no change can be justified.
</p>
</blockquote>


<p id="res-799"><b>Proposed resolution:</b></p>
<p>
In 29.5.4.3 <a href="https://wg21.link/rand.eng.mers">[rand.eng.mers]</a>:
</p>

<blockquote>
<p>
Insert at the end of para 2.:
</p>

<blockquote><p>
[<i>Note:</i> The lower <code>r</code> bits of <code>X<sub>i-n</sub></code> do not influence
the state transition and hence should not be compared when comparing two
<code>mersenne_twister_engine</code> objects. <i>-- end note</i>]
</p></blockquote>

<p>
In para 5. change:
</p>

<blockquote><p>
The textual representation of <code>x<sub>i</sub></code> consists of the values of
<code>X<sub>i-n</sub> <ins>bitand ((2<sup>w</sup> - 1) - (2<sup>r</sup> - 1)),  X<sub>i-(n-1)</sub></ins>,
..., X<sub>i-1</sub></code>, in that order.
</p></blockquote>
</blockquote>





</body>
</html>
