public function getArrayKeyByPowers(array $powers)
    {
        if (empty($powers)) {
            throw new InvalidArgumentException('Empty powers set');
        }

        $powersSum = 0;
        foreach ($powers as $power) {
            if ($power < 0) {
                throw new InvalidArgumentException('Negative power found');
            }

            $powersSum += $power;
        }

        if ($powersSum <= 0) {
            throw new InvalidArgumentException('The sum of powers must be positive');
        }

        $randomValue = $this->generator->getFloat(0, $powersSum);
        $currentSum = 0;
        $result = null;
        foreach ($powers as $key => $power) {
            $currentSum += $power;
            if ($currentSum >= $randomValue) {
                $result = $key;
                break;
            }
        }

        return $key;
    }
public function getArrayKeyByPowers(array $powers)
    {
        if (empty($powers)) {
            throw new InvalidArgumentException('Empty powers set');
        }

        $powersSum = 0;
        foreach ($powers as $power) {
            if ($power < 0) {
                throw new InvalidArgumentException('Negative power found');
            }

            $powersSum += $power;
        }

        if ($powersSum <= 0) {
            throw new InvalidArgumentException('The sum of powers must be positive');
        }

        $randomValue = $this->generator->getFloat(0, $powersSum);
        $currentSum = 0;
        $result = null;
        foreach ($powers as $key => $power) {
            $currentSum += $power;
            if ($currentSum >= $randomValue) {
                $result = $key;
                break;
            }
        }

        return $key;
    }
public function getArrayKeyByPowers ( array $ powers ) { if ( empty ( $ powers ) ) { throw new InvalidArgumentException ( 'Empty powers set' ) ; } $ powersSum = 0 ; foreach ( $ powers as $ power ) { if ( $ power < 0 ) { throw new InvalidArgumentException ( 'Negative power found' ) ; } $ powersSum += $ power ; } if ( $ powersSum <= 0 ) { throw new InvalidArgumentException ( 'The sum of powers must be positive' ) ; } $ randomValue = $ this -> generator -> getFloat ( 0 , $ powersSum ) ; $ currentSum = 0 ; $ result = null ; foreach ( $ powers as $ key => $ power ) { $ currentSum += $ power ; if ( $ currentSum >= $ randomValue ) { $ result = $ key ; break ; } } return $ key ; }
Returns key of the input array based on the "powers" of array values.

@param array $powers Array of non-negative float values.
Each key's "power" means likeness of this key
being returned.

@return mixed A key of the input array.

@throws InvalidArgumentException for empty input or invalid power values.
Returns key of the input array based on the powers of array values .