問題文: パーセンテージを一致させて、さまざまなコマンドをランダムに実行します。CommandA を 50% の時間で実行し、commandB を 25% の時間で実行し、commandC を 15% の時間で実行するなど、合計パーセンテージは 100% にする必要があります。
私の質問は- CommandA A% の時間、CommandB B% の時間、CommandC C% の時間 ----- CommandZ Z% の時間を実行します。そして、合計パーセンテージは100%である必要があり、最後に、合計実行が完了した後、各コマンドが実行されている回数と各コマンドのパーセンテージ(パーセンテージで各コマンドが実行されている回数を意味します)を確認できます.
アイデア:- 1 から 100 までの乱数を生成し、パーセンテージのいずれかがカテゴリに該当するかどうかを確認します。
List<Double> comamndDistribution = new ArrayList<Double>();
/* Commands to execute. Here I have Z command
* and total percentage should be 100% (A+B+C+D+...+Z=100%)
*/
comamndDistribution.add(A%); // command A
comamndDistribution.add(B%); // command B
comamndDistribution.add(C%); // command C
comamndDistribution.add(D%); // command D
comamndDistribution.add(E%); // command E
-----------
-----------
comamndDistribution.add(Z%); // command Z
private Command getNextCommandToExecute() {
for (int i=0; i < 10000; i++) {
// generating a random number between 1 and 100
int random = r.nextInt(100-1) + 1;
/* My Question is- Execute CommandA A% of time, CommandB B%
of time, CommandC C% of time ----- Command Z Z% of time.
And total percentage should be 100% and at the end I can see
how much times each command is being executed and what is
the percentage of each command(means how many times each command is
being executed in terms of percentage) after total execution is complete.
*/
}
}
/* Get the next command to execute by maintaining the Percentage of
each command randomly*/
Command nextCommand = getNextCommandToExecute();
もっと明確にしましょう- 私の質問は- CommandA A% の時間、CommandB B% の時間、CommandC C% の時間 ----- コマンド NN% の時間を乱数を使用して実行します。また、合計パーセンテージは 100% である必要があります。
PS: この質問は何度か聞かれたと思いますが、私が望んでいた方法ではありません。そこで、これまでのコードを投稿することで、もう一度質問として入れようと思いました。
更新:-人々がより理解できるように、別のロジックで書いた以前のコードを削除して質問を更新しました。