title: Python - modelling probability
body: <p>I have a simple problem. I need a way to make a function which generates 0s in p percent cases and 1s in all other cases. I tried doing it with random.random() like this:</p>
  
  <pre><code>p = 0.40
  
  def generate():
      x = random.random()
      if x &lt; p:
          return 0
      else:
          return 1
  </code></pre>
  
  <p>However, this doesn't seem like a good way. Or it is?</p>