游戏获胜者的利润对数分布

时间:2013-05-30 18:46:19

标签: c# sql math polynomial-math logarithm

我有一个给予,当它完成时,有一个球员和他们的分数表。 另一方面,我有一大笔钱,我想在这些获奖者中分配。我正在寻找一个SQL查询或一些C#代码来做到这一点。

降序排序表如下所示:

UserId | Name | Score | Position | % of winnings | abs. winnings $
00579  | John | 754   | 1        |  ?            | 500 $    
98983  | Sam  | 733   | 2        |  ?            | ?    
29837  | Rick | 654   | 3        |  ?            | ?    <- there are 2 3rd places
21123  | Hank | 654   | 3        |  ?            | ?    <- there are 2 3rd places
99821  | Buck | 521   | 5        |  ?            | ?    <- there is no 4th, because of the 2 3rd places
92831  | Joe  | 439   | 6        |  ?            | ?    <- there are 2 6rd places 
99281  | Jack | 439   | 6        |  ?            | ?    <- there are 2 6rd places 
12345  | Hal  | 412   | 8        |  ?            | ?    
98112  | Mick | 381   | 9        |  ?            | ?    
and so on, until position 50 
98484  | Sue  | 142   | 50       |  ?            | 5 $ 

请注意第3和第6位。

现在我想在前50个职位中分配(虚拟)资金总额(10,000美元)。 (如果分配的位置(现在是50)可以是一个变量,那将是很好的。)

最大和最小量(对于nr 1和nr 50)固定为500和5.

有没有人对SQL查询或C#代码有好主意,以正确填充%的奖金绝对奖金$ 的列?

我更喜欢这样的分布看起来有点对数:(这使得较高的位置比较低的位置相对更多)。

.
|.
| .
|  .
|   .
|     .
|        .
|            .
|                  .
|                           .

2 个答案:

答案 0 :(得分:2)

我从1994年开始没有做过SQL,但我喜欢C#:-)。以下可能适用,根据需要调整DistributeWinPot.DistributeWinPot(...)的参数:

private class DistributeWinPot {

    private static double[] GetWinAmounts(int[] psns, double TotWinAmounts, double HighWeight, double LowWeight) {
        double[] retval = new double[psns.Length];
        double fac = -Math.Log(HighWeight / LowWeight) / (psns.Length - 1), sum = 0;
        for (int i = 0; i < psns.Length; i++) {
            sum += retval[i] = (i == 0 || psns[i] > psns[i - 1] ? HighWeight * Math.Exp(fac * (i - 1)) : retval[i - 1]);
        }
        double scaling = TotWinAmounts / sum;
        for (int i = 0; i < psns.Length; i++) {
            retval[i] *= scaling;
        }
        return retval;
    }

    public static void main(string[] args) {
        // set up dummy data, positions in an int array
        int[] psns = new int[50];
        for (int i = 0; i < psns.Length; i++) {
            psns[i] = i+1;
        }
        psns[3] = 3;
        psns[6] = 6;
        double[] WinAmounts = GetWinAmounts(psns, 10000, 500, 5);
        for (int i = 0; i < psns.Length; i++) {
            System.Diagnostics.Trace.WriteLine((i + 1) + "," + psns[i] + "," + string.Format("{0:F2}", WinAmounts[i]));
        }
    }
}

该代码的输出是:

1,1,894.70
2,2,814.44
3,3,741.38
4,3,741.38
5,5,614.34
6,6,559.24
7,6,559.24
8,8,463.41
9,9,421.84
10,10,384.00
11,11,349.55
12,12,318.20
13,13,289.65
14,14,263.67
15,15,240.02
16,16,218.49
17,17,198.89
18,18,181.05
19,19,164.81
20,20,150.03
21,21,136.57
22,22,124.32
23,23,113.17
24,24,103.02
25,25,93.77
26,26,85.36
27,27,77.71
28,28,70.74
29,29,64.39
30,30,58.61
31,31,53.36
32,32,48.57
33,33,44.21
34,34,40.25
35,35,36.64
36,36,33.35
37,37,30.36
38,38,27.64
39,39,25.16
40,40,22.90
41,41,20.85
42,42,18.98
43,43,17.27
44,44,15.72
45,45,14.31
46,46,13.03
47,47,11.86
48,48,10.80
49,49,9.83
50,50,8.95

答案 1 :(得分:0)

那怎么样?

Select userid, log(score),
   10000 * log(score) / 
      (Select Sum(log(score)) 
       From TableName
       Where score >=
            (Select Min(score) 
             from (Select top 50 score     
                   From TableName
                   Order By score desc) z)) 
From TableName
Order By score desc