code: def unit_choice(chooser_ids, alternative_ids, probabilities):
      
      chooser_ids = np.asanyarray(chooser_ids)
      alternative_ids = np.asanyarray(alternative_ids)
      probabilities = np.asanyarray(probabilities)
  
      logger.debug(
          'start: unit choice with {} choosers and {} alternatives'.format(
              len(chooser_ids), len(alternative_ids)))
  
      choices = pd.Series(index=chooser_ids)
  
      if probabilities.sum() == 0:
          # return all nan if there are no available units
          return choices
  
      # probabilities need to sum to 1 for np.random.choice
      probabilities = probabilities / probabilities.sum()
  
      # need to see if there are as many available alternatives as choosers
      n_available = np.count_nonzero(probabilities)
      n_choosers = len(chooser_ids)
      n_to_choose = n_choosers if n_choosers < n_available else n_available
  
      chosen = np.random.choice(
          alternative_ids, size=n_to_choose, replace=False, p=probabilities)
  
      # if there are fewer available units than choosers we need to pick
      # which choosers get a unit
      if n_to_choose == n_available:
          chooser_ids = np.random.choice(
              chooser_ids, size=n_to_choose, replace=False)
  
      choices[chooser_ids] = chosen
  
      logger.debug('finish: unit choice')
      return choices
docs: Have a set of choosers choose from among alternatives according
      to a probability distribution. Choice is binary: each
      alternative can only be chosen once.
  
      Parameters
      ----------
      chooser_ids : 1d array_like
          Array of IDs of the agents that are making choices.
      alternative_ids : 1d array_like
          Array of IDs of alternatives among which agents are making choices.
      probabilities : 1d array_like
          The probability that an agent will choose an alternative.
          Must be the same shape as `alternative_ids`. Unavailable
          alternatives should have a probability of 0.
  
      Returns
      -------
      choices : pandas.Series
          Mapping of chooser ID to alternative ID. Some choosers
          will map to a nan value when there are not enough alternatives
          for all the choosers.