Randomly selecting an object with a weighted probability in Python
<blockquote>
  <p><strong>Possible Duplicates:</strong><br>
  <a href="http://stackoverflow.com/questions/2073235/random-weighted-choice">Random weighted choice</a><br>
  <a href="http://stackoverflow.com/questions/2140787/select-random-k-elements-from-a-list-whose-elements-have-weights">Select random k elements from a list whose elements have weights</a>  </p>
</blockquote>



<p>Let's say I have a set of N objects,</p>

<pre><code>set = range(a,b) = [a,a+1,...b-2, b-1]
</code></pre>

<p>To select M number of objects with probability 1/len(N), I can do:</p>

<pre><code>random.sample(set, M).
</code></pre>

<p>I want to able to select objects at random with a weighted probability. For example, if not a multiple of 3 or 5, weighted probability will be 1. If a multiples of 3, weighted probability will be 3, if a multiple of 5, weighted probability will be 5, if a multiple of 15, weighted probability will be 15.</p>

<p>To illustrate:</p>

<pre><code>set = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
weighted_set=[(1, weight=1), (2, weight=1), (3, weight=3),(4, weight=1), (5, weight=5),...(15, weight=15)]
random.sample(weighted_set, N)
</code></pre>

<p>How would I do something like this? Thank you.</p>
