title: Best practice: testing code which uses a random number generator
body: <p>Say somewhere (deep, deep down) in the code I am using random numbers to do something¹ cool. Tests should in general be deterministic, while the behavior of the release version of this part of the code explicitly makes use of the non-determinism/randomness of the random number generator.</p>
  
  <p>So to get deterministic (functional) tests, I would like to set a fixed seed value:</p>
  
  <pre><code>size_t seed = 42;
  std::mt19937 rng;
  rng.seed(seed);
  </code></pre>
  
  <p>But I also want to make sure that nothing strange (i.e., an exception) happens when I use random input and a random seed:</p>
  
  <pre><code>std::mt19937 rng;
  rng.seed(std::random_device()());
  </code></pre>
  
  <p>Obviously, a finite number of test runs can not ascertain that the code is correct; however, a large number of test runs can at least give you some confidence. </p>
  
  <p>How do I best handle this? I thought about adding something like:</p>
  
  <pre><code>size_t seed = std::random_device()();
  #ifdef TESTING
      seed = 42;
  #endif
  rng.seed(seed);
  </code></pre>
  
  <p>but then, I can not have one test file (using gtest) that uses a random seed for some tests and a constant seed for some other tests (can I?).</p>
  
  <p>¹ In my case: I choose one element from n uniformly and independently at random to partition the input.</p>
  
  <p>EDIT: I am asking about functional tests, not unit tests.</p>