动态锻炼累积赔率

Dynamically workout accumulative odds

我正在做一个复制流行博彩网站上使用的系统的个人项目,我目前正在做的系统部分是您投注单中特定多重彩的累积赔率。

我给你看可能更容易解释,下面是我的投注单中四个投注的赔率:

2.00   
2.00   
4.00   
10.00

累积赔率是通过将赔率相乘得出的,这意味着如果我对上述所有四个投注进行投注 'winning' 那么我将投注一个值,比如 10 个积分return 是我本金的 160 倍 (2 x 2 x 4 x 10)。

我现在需要计算赔率,如果我要在上述赌注上投注 'treble',例如我只需要以上三项投注中赢即可获得 return,这意味着我需要执行以下操作:

2 x 2 x 4 = 16   
2 x 2 x 10 = 40   
2 x 4 x 10 = 80   
2 x 4 x 10 = 80   

这是四个单独的赌注,所以我的 10 个积分现在变成了 40 个积分,因为我将在上述每个事件中获得 10 个积分。

最好的情况是上述所有投注都赢了,这将使我获得 return 216 倍本金(16 + 40 + 80 + 80)。

下一部分是我坚持的部分,如果我要计算 'doubles':

2 x 2 = 4   
2 x 4 = 8   
2 x 10 = 20   
2 x 4 = 8   
2 x 10 = 20   
4 x 10 = 40

我知道累积赔率是 100,但我很难在 PHP 中写这个,我也让我的代码膨胀到现在我不满意的地步,我知道可能有一种简单的方法可以做到这一点,但我似乎无法弄清楚。

我在代码中可用的信息是赔率数组和变量中的 'multiple amount',例如对于三倍,我有数字 3,对于双倍,我有数字 2。这需要是动态的,这样高达 20 倍的投注(一张投注单中 20 次投注)将能够有他们的双倍,三倍,四-折,五折,等等。

$multiple = 2; 

/*
    $multiple is equal to 2, which indicates that I need to return the doubles odds.
    This number could be any number up to 19 & never higher than count($odds).
*/

$odds = array(
    2.00, 
    2.00, 
    4.00, 
    10.00 
);

/*
    This array is will be passed as an argument in my function.
    It will contain up to 20 different odds.
*/

if($multiple < count($odds)) {

    /*
        This is where I need to work out the accumulative odds of doubles, trebles etc dynamically.

        If correct, code should return:
        100 for doubles
        216 for trebles

        It should also have the ability to return the odds of:
        four-folds from up to 20 bets
        five-folds up to 20 bets
        etc..
    */

} else {

    foreach($betOdds as $key => $betOdd) {
        ($odds == 0.00 ? $odds = $betOdd : $odds *= $betOdd);
    }

}

return $odds;

我也知道我可能没有很好地解释这一点,所以请随时要求任何澄清。

你需要的在数学上叫做"combinations"。这样的问题has already been asked,但是提供的答案告诉了如何在字符串中进行数字组合。这是数组版本:

(注意:为了提高可读性,我将使用 26410

class Combinations implements Iterator
{
    protected $c = null;
    protected $s = null;
    protected $n = 0;
    protected $k = 0;
    protected $pos = 0;

    function __construct($s, $k) {
        if(is_array($s)) {
            $this->s = array_values($s);
            $this->n = count($this->s);
        } else {
            $this->s = (string) $s;
            $this->n = strlen($this->s);
        }
        $this->k = $k;
        $this->rewind();
    }
    function key() {
        return $this->pos;
    }
    function current() {
        $r = array();
        for($i = 0; $i < $this->k; $i++)
            $r[] = $this->s[$this->c[$i]];
        return is_array($this->s) ? $r : implode('', $r);
    }
    function next() {
        if($this->_next())
            $this->pos++;
        else
            $this->pos = -1;
    }
    function rewind() {
        $this->c = range(0, $this->k);
        $this->pos = 0;
    }
    function valid() {
        return $this->pos >= 0;
    }

    protected function _next() {
        $i = $this->k - 1;
        while ($i >= 0 && $this->c[$i] == $this->n - $this->k + $i)
            $i--;
        if($i < 0)
            return false;
        $this->c[$i]++;
        while($i++ < $this->k - 1)
            $this->c[$i] = $this->c[$i - 1] + 1;
        return true;
    }
}

现在,如果您 运行 以下循环:

foreach(new Combinations(["2", "6", "4", "10"], 3) as $combination)
{
    var_dump($combination);
}

你会得到想要的结果:

array(3) {
  [0]=>
  string(1) "2"
  [1]=>
  string(1) "6"
  [2]=>
  string(1) "4"
}
array(3) {
  [0]=>
  string(1) "2"
  [1]=>
  string(1) "6"
  [2]=>
  string(2) "10"
}
array(3) {
  [0]=>
  string(1) "2"
  [1]=>
  string(1) "4"
  [2]=>
  string(2) "10"
}
array(3) {
  [0]=>
  string(1) "6"
  [1]=>
  string(1) "4"
  [2]=>
  string(2) "10"
}

或者,您可以立即得到结果数组元素的乘积:

foreach(new Combinations(["2", "6", "4", "10"], 3) as $combination)
{
    var_dump(array_product($combination));
}

这将导致以下结果:

int(48) // because 2 * 6 * 4 = 48 
int(120) // because 2 * 6 * 10 = 120
int(80) // because 2 * 4 * 10 = 80
int(240) // because 6 * 4 * 10 = 240