title: Are there ways to randomly sample among ties in the R function which.max()?
body: <p>I currently am using the <code>which.max()</code> function in <code>R</code> within a loop. Sometimes, I have a vector which contains the same elements, like:</p>
  
  <pre><code>vec &lt;- c(0,0,2,0,2)
  </code></pre>
  
  <p>The function will then always return:</p>
  
  <pre><code>&gt; which.max(vec)
  [1] 3
  </code></pre>
  
  <p>I am wondering if there is a simple solution to break ties randomly so that it doesn't always choose the smallest index among ties. I know that there is a <code>which.is.max</code> function in <code>nnet</code>, but was hoping to see if there was another simple solution without having to resort to installing extra packages. Thanks.</p>