Note that there are some explanatory texts on larger screens.

plurals
  1. POShould I call reset() on my C++ std random distribution to clear hidden state?
    primarykey
    data
    text
    <p>I would like to wrap the random number distributions from the C++11 standard library with simple functions that take as arguments the distribution's parameters and a generator instance. For example:</p> <pre><code>double normal(double mean, double sd, std::mt19937_64&amp; generator) { static std::normal_distribution&lt;double&gt; dist; return dist(generator, std::normal_distribution&lt;double&gt;::param_type(mean, sd)); } </code></pre> <p>I want to avoid any hidden state within the distribution object so that each call to this wrapper function only depends on the given arguments. (Potentially, each call to this function could take a different generator instance.) Ideally, I would make the distribution instance <code>static const</code> to ensure this; however, the distribution's <code>operator()</code> is not a const function, so this isn't possible.</p> <p>My question is this: To ensure there is no hidden state within the distribution, is it 1) necessary and 2) sufficient to call <code>reset()</code> on the distribution each time? For example:</p> <pre><code>double normal(double mean, double sd, std::mt19937_64&amp; generator) { static std::normal_distribution&lt;double&gt; dist; dist.reset(); return dist(generator, std::normal_distribution&lt;double&gt;::param_type(mean, sd)); } </code></pre> <p>(Overall, I'm confused about the purpose of the <code>reset()</code> function for the random distributions... I understand why the generator would need to be reset/reseeded at times, but why would the distribution object need to be reset?)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. COThanks for the very helpful answers and comments everyone!
      singulars
    2. COI think I'll change my overall strategy after reading the responses: I'll either bind my generator and distribution (which seems to be the intended usage, rather than allowing multiple generators to use the same distribution object), or I may just use the std lib's generators but ignore the distribution functions (after finding out that the distributions are not necessarily portable: http://stackoverflow.com/questions/14840901/is-the-random-library-in-c11-portable, so, even with the same seed, I may get different sequences of e.g. normal distribution samples on different platforms).
      singulars
    3. COIt seems that there are two use cases here: 1) binding an engine and distribution (the intended use), and 2) using multiple engines with one distribution (what I am doing here). I suppose I prefer to think of the distributions as simple stateless functions: you give them an engine, and they return a sample. But I understand the need to treat them as objects so they can cache values for efficiency. It would have been nice if the standard provided, for each distribution, both a stateless function to get just *one* sample, and an object for generating/caching *several* samples.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload