dpbf62565 2017-01-31 16:19
浏览 62
已采纳

动态锻炼累积赔率

I'm working on a personal project that replicates the systems used on popular betting websites, the part of the system I am working on at the moment is the accumulative odds of particular accumulators in your betslip.

It may be easier for me to explain by showing you, below are the odds of the four bets in my betslip:

2.00   
2.00   
4.00   
10.00

The accumulative odds are worked out by multiplying the odds together, this means that if I were to place a bet on all four of the above bets 'winning' then I would stake one value, say 10 credits for a return that is 160x my stake (2 x 2 x 4 x 10).

I now need to work out the odds if I were to place a 'treble' stake on the above bets, e.g. I only need three of the above bets to win in order to get a return, this means I need to do the following:

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

That is four separate bets, so my stake of 10 credits now becomes 40 credits because I will have 10 credits on each of the above happening.

The best case scenario is that all of those above bets win, which would grant me return of 216x my stake (16 + 40 + 80 + 80).

The next part is the part I am stuck on, if I were to work out the '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

I know that the accumulative odds is 100, but I am struggling to write this in PHP, I have also bloated my code up to a point now where I'm not happy with it, I know there is likely to be a simple way to do this but I can't seem to figure it out.

The information I have available in my code is an array of the odds and the 'multiple amount' in a varialbe, e.g. for the trebles I have the number 3, for the doubles I have the number 2. This needs to be dynamic so that bets as high as 20 folds (20 bets in a betslip) will be able to have their double, treble, four-fold, five-fold, etc worked out.

$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;

I'm also aware I've probably not explained this very well so feel free to ask for any clarification on anything.

  • 写回答

1条回答 默认 最新

  • dongshi1880 2017-01-31 16:31
    关注

    What you need is called "combinations" in mathematics. Such a question has already been asked, but the answer provided tells how to make a combination of numbers in string. This is the array version:

    (NOTE: I will use 2, 6, 4 and 10 for my example to be more readable)

    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;
        }
    }
    

    Now, if you run the following loop:

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

    you will get desired results:

    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"
    }
    

    Alternatively, you may get the product of result array elements right away:

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

    This will result in the following:

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用