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)


上面是我的文本文件,通过从StackOverflow此处获取建议,我在下面编写的逻辑中70%的时间打印CommandA,而30%的时间打印CommandB。现在我想要的是,如果CommandA在70%的时间内被打印,那么在70 $的时间内80%的时间,它也应该打印上一个,而在70%的时间中应该打印New的20%。同样,它应打印70%的时间为true的50%和false的时间为50%。
所以基本上问题是这样的-问题陈述




70%的时间打印“ CommandA”,其中70%的时间打印80%
“上一个”并打印20%的“新”。在这70%的人中,有50%的人表示“真实”
并打印50%“ false”。同样,对于CommandB,打印“ CommandB”的30%
时间,在这30%中,打印50%“上一个”,然后打印50%“新”。
在这30%中,打印出30%为“真”,打印出70%为“假”




因此,目前在下面的代码中,我正在打印70%的CommandA和30%的CommandB。我不确定如何为上述要求添加代码。

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);
}


附言我为执行百分比而编写的逻辑来自我在stackoverflow上所做的发布。

最佳答案

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

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


生成一个新数字:(请参见上面的生成方法),然后可以将百分比与所需的第二级分布进行比较(例如,Previous为80)
重用相同的数字:计算命令选择阈值的适当部分,然后将其与该数字进行比较。例如。对于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();
  }
}


注意:上面的代码假定所有CommandgetExecutionPercentage()的总和(至少)为100

更新:由于方法不是静态的,所以制作了一个Random对象

关于java - 随机分配百分比给每个单词,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10694638/

10-11 19:24