title: Get a random element from an array in a hash
body: <p>I have seen a lot of results on google on how to get a random array index, but I have not been able to apply it to this scenario.</p>
  
  <p>Consider the following: </p>
  
  <pre><code>my %hash;
  my @array = {"foo", "bar", "poo"};
  
  $hash-&gt;{mykey} = @array;
  </code></pre>
  
  <p>How would I get a random element from the array inside $hash->{mykey}? Something like the following code which does not work:</p>
  
  <pre><code>my $element = $hash-&gt;{mykey}[rand($hash-&gt;{mykey})];
  </code></pre>
  
  <p>EDIT: So the answers below are extremely informative for this. Compounding <em>my</em> issue in particular was that I was using the threads module, and completely forgot to share the arrays that I was appending to the hash elements! Due to this, the answers were not working for me right away. </p>
  
  <p>After fixing that oversight, the solutions below worked perfectly. </p>