public static function getWeighted(array $values) :string {

        $currentTotal = 0;
        $firstRand = random_int(1, 100);

        $total = array_sum($values);

        $rand = ($firstRand / 100) * $total;

        foreach ($values as $key => $weight) {
            $currentTotal += $weight;

            if ($rand <= $currentTotal) {
                return $key;
            }
        }

        return '';
    }
public static function getWeighted(array $values) :string {

        $currentTotal = 0;
        $firstRand = random_int(1, 100);

        $total = array_sum($values);

        $rand = ($firstRand / 100) * $total;

        foreach ($values as $key => $weight) {
            $currentTotal += $weight;

            if ($rand <= $currentTotal) {
                return $key;
            }
        }

        return '';
    }
public static function getWeighted ( array $ values ) : string { $ currentTotal = 0 ; $ firstRand = random_int ( 1 , 100 ) ; $ total = array_sum ( $ values ) ; $ rand = ( $ firstRand / 100 ) * $ total ; foreach ( $ values as $ key => $ weight ) { $ currentTotal += $ weight ; if ( $ rand <= $ currentTotal ) { return $ key ; } } return '' ; }
Get one element out of an input array with specified weights to get the distribution of the generated elements as you want them.

@param array $values Input array with values as key and weight as value. ['value 1' => 30, 'value 7' => 70]

@return string
@throws \Exception
Get one element out of an input array with specified weights to get the distribution of the generated elements as you want them .