Truncating a random gammavariate result to an upper limit
<p>I want to draw a number from a gammavariate, but I want to set an upper limit.
Why this does not work?</p>

<pre><code>import random
[int(random.gammavariate(3, 3)) if x &lt; 21 else 20 for x in range(1)]

Out[59]: [22]
</code></pre>

<p>Thanks.</p>

<p>Why making things <a href="https://www.python.org/dev/peps/pep-0020/" rel="nofollow">complicated</a>?</p>

<p><code>x</code> is always <code>0</code> in your list comprehension.</p>

<p>try it this way:</p>

<pre><code>x = int(random.gammavariate(3,3))
[x if x &lt; 21 else 20]
</code></pre>

python random