title: How can I store the state of the pseudo-random generator in Perl?
body: <p>Is there a way to store the current state of the built in pseudo-random number generator in Perl so that when my program is run again, it can pick up the sequence from where it left off rather than starting with a new sequence?</p>
  
  <p>Right now, I am storing where I am as well as the initial seed and then throwing away the initial segment which I have already seen using something similar to:</p>
  
  <pre><code>sub consume_upto_n {
      my ($seed, $n) = @_;
      $n = 1 unless defined $n and $n &gt;= 1;
      srand $seed;
      rand for 1 .. $n - 1;
      return;
  }
  </code></pre>
  
  <p>For example:</p>
  
  <pre><code>srand 0x18;
  my @v = map { rand } 1 .. 5;
  </code></pre>
  
  <p>Later:</p>
  
  <pre><code>consume_upto_n(0x18, 3);
  my @z = map { rand } 3 .. 5;
  </code></pre>
  
  <p>Then, <code>$z[0] == $v[2]</code>, <code>$z[1] == $v[3]</code> etc.</p>