0

レースのように、勝者を選ぶ方法を見つける必要があります。各レーサーには、勝つチャンスの重みがあります..

例えば

A - 25% chance to win
B - 10% chance to win
C - 25% chance to win
D - 10% chance
E - 10% chance
F - 20% chance

1) 勝者をランダムに選ぶ必要がありますが、重み付けを考慮する必要があります。

2) また、2 番目に最も可能性の高い勝者と 3 番目の勝者を選ぶ必要があります...

1 から 100 の間でランダムな勝者を生成し、基本的に次のような可能性に応じてレーサー間で 100 を分割することで、最初の勝者を選ぶことができます。

A = 1-25
B = 26 -35
C = 36-60.
etc
etc

上記の安定性はわかりませんが、問題ないようでした。

他にアイデアはありますか?

4

2 に答える 2

0
public class RaceWinners{
    public static void main(String[] args) {
        int[] percentages = {25,10,25,10,10,20};
        System.out.print(pickWinner(percentages));
    }

    //Returns the index of the winner(A = 0, B = 1, ...)
    private static int pickWinner(int[] percentages) {
        int randomNumber = (int)(Math.random() * 100);
        int countdown = randomNumber;
        for(int i = 0; i < percentages.length; i++) {
            countdown -= percentages[i];
            if(countdown < 0) {
                return i;
            }
        }
        return -1;
   }

}
于 2012-10-12T15:23:30.717 に答える
0

To determine the first rank you can do as you have described. But for the second, third,... places are little bit different (if A is first, then B has probability to be second p(B|A) := "the second is B given that the first is A", then B has not 10% to be second but a different value to be second and so on for the others members). Take a look at the condition probability (wiki: http://en.wikipedia.org/wiki/Conditional_probability )

this is a "pseudocode" that do the job

Ranking:
begin probability
a = 20%
b = 10%
...

calculate first
recalculate probability given that first is X (e.g. if first is A p(B|A), p(C|A),...)
calculate second (with the new probability, e.g. b = p(B|A), c = p(C|A),..)
recalculate probability given that first is X and second is Y
calculate third
etc..

NOTE: java random (Math.random) generates value between 0 and 0.999999... (e.g. [0, 1[ 0 is included but 1 not)

于 2012-10-12T15:28:57.583 に答える