Java 使用随机数执行命令A时间百分比、命令B时间百分比、命令C时间百分比------命令Z时间百分比

Java 使用随机数执行命令A时间百分比、命令B时间百分比、命令C时间百分比------命令Z时间百分比,java,random,Java,Random,问题陈述:通过匹配其百分比随机执行各种命令。 如执行CommandA 50%的时间和commandB 25%的时间和commandC 15%的时间等,总百分比应为100% 我的问题是-执行命令A%的时间,命令B% 时间的百分比,CommandC时间的百分比--CommandZ时间的百分比。 总的百分比应该是100%,最后我可以看到 每个命令执行多少次,执行的时间是多少 每个命令的百分比表示每个命令执行的次数 以总执行完成后的百分比表示正在执行 想法:- 生成一个介于1和100之间的随机数,看看是

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

我的问题是-执行命令A%的时间,命令B% 时间的百分比,CommandC时间的百分比--CommandZ时间的百分比。 总的百分比应该是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%——commandn时间的N%。总的百分比应该是100%

我想这个问题已经被问了好几次了,但这不是我想要的方式。所以我想把它再次作为一个问题,发布我的代码,我到目前为止做了什么


更新:-我已经更新了这个问题,删除了以前用另一种逻辑编写的代码,以便人们能够更好地理解它。

这不是你问的问题,但我认为这是你的意思

class RandomCommands {
  // A Map that associates each command with a threshold from 0 - totalWeights.
  // The order of iteration is the same as the order of insertion.
  private Map<int, Command> commands = new LinkedHashMap<int, Command>>();

  // Running total of the weights of all of the commands.  The likelihood that
  // particular command will be executed is its weight / totalWeights.
  private int totalWeights = 0;

  // Adds a command with the specified weight, which can be any positive integer.
  public void addCommand(int weight, Command command) {
    commands.put(totalWeights, command);
    totalWeights += weight;
  }

  // Chooses a random command.
  public Command getRandomCommand() {
    // Choose a random number to select a command.
    int selector = Random.nextInt(totalWeights);

    // Find the corresponding command.
    for (Element<int, Command> command : commands) {
      if (command.key >= selector) {
        return command.value;
      }
      selector -= command.key;
    }
  }
}
每次调用getRandomCommand时,它都会选择一个从0到79的随机数。它将返回以下命令之一:

selector command frequency of execution ———————— ——————— ——————————————————————————————————————— 0 - 19 Command1 (1 + 19 - 0) / 80 = 20/80 = 1/4 = 25% 20 - 39 Command2 (1 + 39 - 20) / 80 = 20/80 = 1/4 = 25% 40 - 79 Command3 (1 + 79 - 40) / 80 = 40/80 = 1/2 = 50%
这不是你问的,但我想这是你的意思

class RandomCommands {
  // A Map that associates each command with a threshold from 0 - totalWeights.
  // The order of iteration is the same as the order of insertion.
  private Map<int, Command> commands = new LinkedHashMap<int, Command>>();

  // Running total of the weights of all of the commands.  The likelihood that
  // particular command will be executed is its weight / totalWeights.
  private int totalWeights = 0;

  // Adds a command with the specified weight, which can be any positive integer.
  public void addCommand(int weight, Command command) {
    commands.put(totalWeights, command);
    totalWeights += weight;
  }

  // Chooses a random command.
  public Command getRandomCommand() {
    // Choose a random number to select a command.
    int selector = Random.nextInt(totalWeights);

    // Find the corresponding command.
    for (Element<int, Command> command : commands) {
      if (command.key >= selector) {
        return command.value;
      }
      selector -= command.key;
    }
  }
}
每次调用getRandomCommand时,它都会选择一个从0到79的随机数。它将返回以下命令之一:

selector command frequency of execution ———————— ——————— ——————————————————————————————————————— 0 - 19 Command1 (1 + 19 - 0) / 80 = 20/80 = 1/4 = 25% 20 - 39 Command2 (1 + 39 - 20) / 80 = 20/80 = 1/4 = 25% 40 - 79 Command3 (1 + 79 - 40) / 80 = 40/80 = 1/2 = 50%
我和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();
    }

  }
}
当然,一般来说,每次跑步都会有不同的计数

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

我和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();
    }

  }
}
当然,一般来说,每次跑步都会有不同的计数

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

您可能希望将问题文本发布在帖子正文中,而不是作为代码中的注释,因为代码超出了窗口,很难阅读。谢谢您的建议。我编辑了我的问题这不是家庭作业,我正在做一个项目。如果这是一个家庭作业,我会将其标记为家庭作业。Yu可能会看到所示的方法。它必须用Java完成为什么?您可能希望将问题文本发布在文章的正文中,而不是作为代码中的注释,因为代码超出了窗口,很难阅读。谢谢您的建议。我编辑了我的问题这不是家庭作业,我正在做一个项目。如果这是一个家庭作业,我会将标签作为家庭作业。Yu可能会看到所示的方法。它必须用Java完成为什么?