code: def _weighted_choice(population, weights, random_generator=None):
      """Like random.choice, but with weights.
  
      Heavily inspired by python 3.6 `random.choices`.
      """
      if not population:
          raise ValueError("Can't choose from empty population")
      if len(population) != len(weights):
          raise ValueError(
              "The number of weights does not match the population"
          )
      cum_weights = list(accumulate(weights))
      total = cum_weights[-1]
      threshold = random_generator.random()
      return population[bisect(cum_weights, total * threshold)]
  
imports: import random
  import warnings
  from abc import ABCMeta, abstractmethod
  from bisect import bisect
  from itertools import accumulate
  from nltk.lm.counter import NgramCounter
  from nltk.lm.util import log_base2
  from nltk.lm.vocabulary import Vocabulary
label_desc: Use image node_id 3 for calling a global function with example usage: _weighted_choice(population, weights, random_generator) and returns: population