0
Percentage:70 - CommandA  Data:Previous/New(80/20)    User:true/false(50/50)
Percentage:30 - CommandB  Data:Previous/New(50/50)    User:true/false(30/70)

Above is my Text File in which I am printing CommandA 70% of the time and CommandB 30% of the time from the logic that I wrote below by getting advice here from StackOverflow. Now what I want is that if CommandA is being printed 70% of the time, then 80% of the 70$ of time, it should also prints Previous and 20% of the 70% of time it should print New. Similarly it should print 50% of the 70% of time true and 50% of the time false. So Basically problem is like this- Problem Statement


Print "CommandA" 70% of the time, and out of those 70% print 80% "Previous" and print 20% "New". And out of those 70% print 50% "true" and print 50% "false" Likewise, for CommandB print "CommandB" 30% of the time, and out of those 30% print 50% "Previous" and print 50% "New". And out of those 30% print 30% "true" and print 70% "false"


So currently in my below code I am printing 70% of CommandA and 30% of CommandB. I am not sure how should I add the code for above requirements.

public static void main(String[] args) {
        commands = new LinkedList<Command>();
        values = new ArrayList<String>();
        br = new BufferedReader(new FileReader("S:\\Testing\\Test2.txt"));
        while ((sCurrentLine = br.readLine()) != null) {
            percentage = sCurrentLine.split("-")[0].split(":")[1].trim();
            values = Arrays.asList(sCurrentLine.split("-")[1].trim().split("\\s+"));
            for(String s : values) {
                if(s.contains("Data:")) {
                // Here data contains **Previous/New(80/20)**
                    data = s.split(":")[1];
                } else if(s.contains("User:")) {
                // Here userLogged contains **true/false(50/50)**
                    userLogged = s.split(":")[1];
                } else {
                    cmdName = s;
                }
            }

            Command command = new Command();
            command.setName(cmdName);
            command.setExecutionPercentage(Double.parseDouble(percentage));
            command.setDataCriteria(data);
            command.setUserLogging(userLogged);
            commands.add(command);
        }

        executedFrequency = new Long[commands.size()];

        for (int i=0; i < commands.size(); i++) {
            executedFrequency[i] = 0L;
        }

        for(int i = 1; i < 10000; i++) {
            Command nextCommand = getNextCommandToExecute();
    // So by my logic each command is being printed specified number of percentage times                    
    System.out.println(nextCommand.getName()); 


/*
 * What I want is that if Command A is executed 70% of time, then according 
 * to properties  file 80% times of 70% of CommandA it should print Previous 
 * and 20% times of 70% of CommandA it should print New Likewise same thing 
 * for User. It should print 50% times of 70% of CommandA true and 50% to false.
 * 
 */

        }
    } 

}

// Get the next command to execute based on percentages
private static Command getNextCommandToExecute() {
    int commandWithMaxNegativeOffset = 0; // To initiate, assume the first one has the max negative offset
    if (totalExecuted != 0) {
        // Manipulate that who has max negative offset from its desired execution
        double executedPercentage = ((double)executedFrequency[commandWithMaxNegativeOffset] / (double)totalExecuted) * 100;
        double offsetOfCommandWithMaxNegative = executedPercentage - commands.get(commandWithMaxNegativeOffset).getExecutionPercentage();

        for (int j=1; j < commands.size(); j++) {
            double executedPercentageOfCurrentCommand = ((double)executedFrequency[j] / (double)totalExecuted) * 100;
            double offsetOfCurrentCommand = executedPercentageOfCurrentCommand - commands.get(j).getExecutionPercentage();

            if (offsetOfCurrentCommand < offsetOfCommandWithMaxNegative) {
                offsetOfCommandWithMaxNegative = offsetOfCurrentCommand;
                commandWithMaxNegativeOffset = j;
            }
        }
    }

    // Next command to execute is the one with max negative offset
    executedFrequency[commandWithMaxNegativeOffset] ++;
    totalExecuted ++;

    return commands.get(commandWithMaxNegativeOffset);
}

P.S. The logic that I wrote for percentage execution is from the posting that I did on the stackoverflow.

4

1 回答 1

1

您可以使用java.util.Random该类生成随机数。该Random.nextDouble()方法返回一个介于 0 和 1 之间的值,因此如果将其乘以 100,则会得到一个百分比。然后将数字与命令的所需百分比进行比较(例如 70 表示CommandA

由于您知道命令所需的百分比,您可以生成另一个随机数或使用刚刚生成的随机数来选择命令。

  1. 生成一个新数字:(参见上面的生成),然后您可以将百分比与所需的二级分布(例如 80 Previous)进行比较

  2. 重用相同的数字:计算命令选择阈值的适当部分并将数字与该数字进行比较。例如,CommandA阈值为 70。假设您生成了 69(小于 70,因此CommandA被选中)。所以你计算 70*80%=56。69 大于那个,所以你选择New(而不是Previous

注意:即使您保留当前选择命令的逻辑,您也可以采用方法 1)

更新:代码示例:

Random rnd = new Random();
double percent = rnd.getNextDouble()*100;
for (Command c : commands) {
  if (percent < c.getExecutionPercentage()) {
    // we select the current command
    percent = rnd.getNextDouble()*100;
    if (percent < command.getDataCriteria().getPreviousPercentage()) {
      // we select Previous
    } else {
      // we select New
    }
    break;
  } else {
    percent -= c.getExecutionPercentage();
  }
}

注意:上面的代码假设所有Commands的总和getExecutionPercentage()是(至少)100

更新:制作了一个Random对象,因为方法不是静态的

于 2012-05-22T01:40:16.187 回答