Randomly select one of the elements based on their weights.

@return mixed Selected element
171266
public function lookup() {
        $r = mt_rand() / mt_getrandmax() * $this->_weightTotal;
        return $this->_values[$this->_binarySearch($r, $this->_lookup)];
    }
177234
public function getSelect()
    {
        if (null === $this->_select) {
            $db = $this->getAdapter();

            /**
             * Build select object
             */
            $select = new \Zend_Db_Select($db);
            $select->from('gems__surveys', array('gsu_export_code'))
                    ->where('gsu_export_code = ?')
                    ->where('gsu_id_survey NOT IN (?)', implode(', ', $this->_tested))
                    ->limit(1);

            // \MUtil_Echo::track($select->__toString());
            $this->_select = $select;
        }
        return $this->_select;
    }
874786
public function selected()
    {
        // 请求参数
        $param['__EVENTTARGET'] = '';
        $param['__EVENTARGUMENT'] = '';
        $param['__VIEWSTATE'] = true;
        $param['zymc'] = "";
        $param['xx'] = "";
        $param['Button8'] = iconv('utf-8', 'gb2312', '已选课程');

        $header = [
            'code', 'name', 'property', 'direction', 'credit', 'hour', 'exam_time', '', 'status'
        ];

        return $this->getSelect($param, $param, $header, 10);
    }
119546
def weighted_choice(self, probabilities, key):
        """"""

        total = sum(x[0] for x in probabilities)
        choice = total * self._random(key)

        for probability, option in probabilities:
            choice -= probability
            if choice <= 0:
                return option
575780
public function imgAreaSelect()
    {
        $this->jquery();

        $this->Html->script('libs/img-area-select.min.js', $this->_setOptions([
            'alias'  => __FUNCTION__,
            'weight' => self::WEIGHT_LIB
        ]));

        return $this;
    }
1106532
public function getElements()
    {
        foreach ($this->elements as $name => $element) {
            $this->resolveXRef($name);
        }

        return $this->elements;
    }
528210
protected function getSelectedValue()
    {
        $value = $this->getValueAttribute();
        if (!is_array($value) && !($value instanceof \Traversable)) {
            return $this->getOptionValueScalar($value);
        }
        $selectedValues = [];
        foreach ($value as $selectedValueElement) {
            $selectedValues[] = $this->getOptionValueScalar($selectedValueElement);
        }
        return $selectedValues;
    }
180301
private function getRandomBoard()
    {
        if ($this->boards === null) {
            $this->boards = Board::all();
        }

        return $this->boards->random();
    }
286416
public function getSelector(): string
    {
        if (empty($this->selector)) {
            $this->setSize($this->selector_size);
            $this->selector = $this->generateRandomToken();
        }

        return $this->selector;
    }
559195
public function get_selected_users() {
        // Do a lazy load.
        if (is_null($this->selected)) {
            $this->selected = $this->load_selected_users();
        }
        return $this->selected;
    }
592200
public function hasRandomSort()
    {
        foreach ($this->sortsBy as $sortBy) {
            if (self::TYPE_RANDOM === $sortBy['type']) {
                return true;
            }
        }

        return false;
    }
389832
public function setSelect() {
        $select = $this->_options['select'];
        if (!$select) {
            $select = $this->getModel()->select();
        }
        if ($this->_options['conditions']) {
            $select->where($this->_options['conditions']);
        }       
        if ($this->_options['order']) {
            $select->order($this->_options['order']);
        }
        $this->_select = $select;
        return $this;
    }
315358
protected function _getRandomGenerator()
    {
        return $this->_randomizer ? $this->_randomizer->getArrayValue($this->_generators) : $this->_generators[array_rand($this->_generators)];
    }
159435
protected function runSelect()
    {
        return $this->connection->select(
            $this->toSql(), $this->getBindings(), ! $this->useWritePdo
        );
    }
1206062
public function toBeSelected()
    {
        $this->wait()->until(WebDriverExpectedCondition::elementToBeSelected($this->elementFinder->getSearchCriteria()));

        return $this;
    }
323087
protected function runSelect()
    {
        if ($this->lock) {
            $this->connection->beginTransaction();
            $result = $this->connection->select($this->toSql(), $this->getBindings(), ! $this->useWritePdo);
            $this->connection->commit();

            return $result;
        }

        return $this->connection->select($this->toSql(), $this->getBindings(), ! $this->useWritePdo);
    }
1037889
def weighted_random_choice(items):
    
    l = list(items)
    r = random.random() * sum([i[1] for i in l])
    for x, p in l:
        if p > r:
            return x
        r -= p
    return None