Did I find the right examples for you? yes no

# utils.weighted_choice

All Samples(9)  |  Call(5)  |  Derive(0)  |  Import(4)
```Returns a value from choices chosen by weighted random selection

choices should be a list of (value, weight) tuples.

eg. weighted_choice([('val1', 5), ('val2', 0.3), ('val3', 1)])
```

```        def weighted_choice(choices):
"""Returns a value from choices chosen by weighted random selection

choices should be a list of (value, weight) tuples.

eg. weighted_choice([('val1', 5), ('val2', 0.3), ('val3', 1)])

"""
values, weights = zip(*choices)
total = 0
cum_weights = []
for w in weights:
total += w
cum_weights.append(total)
x = random.uniform(0, total)
i = bisect.bisect(cum_weights, x)
return values[i]
```

```import materials
import perlin
from utils import (
findChunkDepths,
get_entity_mob_tags,
```
```    def nameDungeon(self):
# Name this place
A = weighted_choice(self._dnamesA)
name = weighted_choice(self._dnamesB).format(A=A)
return name
```

```from entities import Asteroid
from geometry.vector import Vector
from utils import weighted_choice, WINDOW_WIDTH, \
WINDOW_HEIGHT, rand_direction

```
```        # Get a random size, with probability depending on the game
# rules
size = weighted_choice(self.curr_level.size_weights)

# Generate a position out of bounds, and a random direction
```

```import os
import sys
import random
from utils import weighted_choice
from namegen import namegen
```
```                             ('norse', 10),
('elven', 10)]
return weighted_choice(theme_weights)

def genname(self):
```

```    while True:
# Pick one.
ench = utils.weighted_choice(prob)
final[ench] = enchantments[ench]
```

```from . import filters
from .helpers import get_headers, status_code, get_dict, check_basic_auth, check_digest_auth, H, ROBOT_TXT, ANGRY_ASCII
from .utils import weighted_choice
from .structures import CaseInsensitiveDict

```
```        choices.append((int(code), float(weight)))

code = weighted_choice(choices)

return status_code(code)
```