title: Is there a better way than this to implement Softmax Action Selection for Reinforcement Learning?
body: <p>I am implementing Softmax Action Selection policy for a reinforcement learning task (<a href="http://www.incompleteideas.net/book/ebook/node17.html" rel="nofollow noreferrer">http://www.incompleteideas.net/book/ebook/node17.html</a>).</p>
  
  <p>I came with this solution, but I think there is room for improvement.</p>
  
  <p>1-Here I evaluate the probabilities </p>
  
  <pre><code>    prob_t = [0]*3
      denominator = 0
      for a in range(nActions):
          denominator += exp(Q[state][a] / temperature) 
  
      for a in range(nActions):
          prob_t[a] = (exp(Q[state][a]/temperature))/denominator  
  </code></pre>
  
  <p>2-Here I am comparing a random generated number in the range ]0,1[ to the probabilities value of the actions:</p>
  
  <pre><code>    rand_action = random.random()
      if rand_action &lt; prob_t[0]:
          action = 0      
      elif rand_action &gt;= prob_t[0] and rand_action &lt; prob_t[1]+prob_t[0]:
          action = 1      
      else: #if rand_action &gt;= prob_t[1]+prob_t[0]
          action = 2
  </code></pre>
  
  <p>edit: </p>
  
  <p>example: rand_action is 0.78, prob_t[0] is 0.25, prob_t[1] is 0.35, prob_t[2] is 0.4.
  the probabilities sum to 1. 
  0.78 is greater than the sum of the probabilities for action 0 and 1 (prob_t[0] + prob_t[1]) therefore action 2 is picked.</p>
  
  <p>Is there a more efficient way of doing this?</p>