title: Generate Sequence of Booleans Quickly in Python
body: <p>I have a for loop that looks like this, and am looking to make it faster.</p>
  
  <p>mylist = range(100)</p>
  
  <pre><code>def normalrandom():
      for a in range(100):
          b = random.getrandbits(1)
          if b==1: #do something with mylist[a]
  </code></pre>
  
  <p>My list has ~100 elements, and I know calls to random are expensive. Is there a faster way to make just one call to random, and get 100 random booleans?</p>
  
  <p>Edit: Here is the best solution so far.</p>
  
  <pre><code>def fastrandom(): 
      s = list(range(100))
      res = [i for i in s if random.random() &gt;= .5]
      for item in res:
          #do something with mylist[item]
  </code></pre>
  
  <ul>
  <li>normalrandom:  0:00:00.591000 </li>
  <li>fastrandom:  0:00:00.293000</li>
  </ul>