Demo entry 6758305

py

   

Submitted by anonymous on Aug 10, 2018 at 16:04
Language: Python 3. Code size: 159 Bytes.

def weighted_choice(weights):
  rnd = random.random() * sum(weights)
  for i, w in enumerate(weights):
      rnd -= w
      if rnd < 0:
          return i

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).