title: Inconsistent results for f(g(x)) together or split up
body: <p>During <a href="https://stackoverflow.com/q/31063419/3093387">a recent investigation</a> into setting random seeds within functions, I came across an odd situation. Consider functions <code>f</code> and <code>g</code>, each of which sets the random seed and then performs a simple randomized operation:</p>
  
  <pre><code>g &lt;- function(size) { set.seed(1) ; runif(size) }
  f &lt;- function(x) { set.seed(2) ; x*runif(length(x)) }
  </code></pre>
  
  <p>Because each function sets the random seed, I would expect each function to always have the same return value given the same input. This would mean <code>f(g(2))</code> should return the same thing as <code>x &lt;- g(2) ; f(x)</code>. To my surprise this is not the case:</p>
  
  <pre><code>f(g(2))
  # [1] 0.1520975 0.3379658
  
  x &lt;- g(2)
  f(x)
  # [1] 0.04908784 0.26137017
  </code></pre>
  
  <p>What is going on here?</p>