gpt4 book ai didi

java - Execute CommandA A% of time, CommandB B% of time, CommandA C% of time ----- Command Z% time 使用随机数

转载 作者:行者123 更新时间:2023-11-29 09:13:56 27 4
gpt4 key购买 nike

问题陈述:通过匹配其百分比随机执行各种命令。比如执行 CommandA 50% 的时间和 commandB 25% 的时间和 commandC 15% 的时间等等,总百分比应该是 100%。

我的问题是 - 执行 CommandA A% 的时间,CommandB B% of time, CommandC C% of time ----- CommandZ Z% of time。 总百分比应该是 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% 的时间 ----- 命令 N N% 的时间通过使用随机数。总百分比应为 100%。

P.S:我想这个问题已经被问过几次了,但这不是我想要的方式。所以我想通过发布我到目前为止所做的代码来再次提出它作为一个问题。

更新:- 我已经通过删除我以前用另一种逻辑编写的代码来更新问题,以便人们可以更多地理解它。

最佳答案

我采用了与 Adam Liss 类似的方式,但结果更加冗长。

同样,我认为您不能依赖所提供的命令总是加起来达到 100%,所以我已经解决了这个问题,但我的方法需要调用一个方法来重新规范化比例(因此更容易出错)。

import java.util.*;

/**
A basic 'Command' class for testing
*/
class Command {
private String id;
public Command (String pId) {
this.id = pId;
}
public void execute () {
System.out.println ("Command: "+id);
}
}

/** The class that does the random selection bit of magic */
public class CommandDist {

/** an internal helper class to manage proportions and the command */
class Cmd {
Command command; // the command that will get executed
double assignedProportion; // weight assigned when added
double cumulativeProportion; // recalculated later to between 0 and 1

public Cmd (Command c, double proportion) {
this.command = c;
this.assignedProportion = proportion;
this.cumulativeProportion = 0.0;
}
}

// the list I'm using
private List<Cmd> commandDistribution = new ArrayList<Cmd>();
private java.util.Random myRandom = new java.util.Random();

void addCommand (Command command, double proportion) {
commandDistribution.add ( new Cmd (command, proportion));
}

// ** MUST BE CALLED **, after adding all the commands, to normalise the proportions.
// you could do this tidier by setting a flag in add, and checking it in
// getNextCommandToExecute
void normaliseProportion() {
double total = 0;
double cumulativeProp = 0;
for (Cmd cmd: commandDistribution) {
total += cmd.assignedProportion;
}
for (Cmd cmd: commandDistribution) {
cumulativeProp += cmd.assignedProportion/total;
cmd.cumulativeProportion = cumulativeProp;
}
}


private Command getNextCommandToExecute () {
double d = myRandom.nextDouble();
for (Cmd cmd: commandDistribution) {
if (d < cmd.cumulativeProportion) {
return cmd.command;
}
}
// theoretically, should not get here. Never rely on theoretically.
return commandDistribution.get(0).command;

}

public static void main (String [] args) {
CommandDist cd = new CommandDist();
Command c;

cd.addCommand (new Command ("A"), 50.0);
cd.addCommand (new Command ("B"), 20.0);
cd.addCommand (new Command ("C"), 15.0);
cd.addCommand (new Command ("D"), 10.0);

cd.normaliseProportion();


for (int i = 0; i < 10000; i++) {
c = cd.getNextCommandToExecute();
c.execute();
}

}
}

输出看起来像这样:

Command: C
Command: A
Command: C
Command: A
Command: D

并且通常这样分布(当然,每次运行的计数不同)。

java CommandDist | sort | uniq -c
5183 Command: A
2151 Command: B
1595 Command: C
1071 Command: D

关于java - Execute CommandA A% of time, CommandB B% of time, CommandA C% of time ----- Command Z% time 使用随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10661647/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com