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)
以上就是我的文本文件中,我打印CommandA的70%的時間和CommandB的從我從StackOverflow的及彼諮詢下面寫的邏輯的時間30% 。現在我想要的是,如果CommandA在70%的時間內打印出來,然後是70%的時間的80%,那麼它還應該打印Previous以及打印New時間的70%時間的20%。同樣,它應該打印70%的真實時間中的50%和50%的時間虛假。 所以基本問題是像這 - 問題陳述隨機分配百分比,以每個詞
打印 「CommandA」 的70%的時間,進出的70%印刷80% 「上一頁」 和印刷20%的 「新」 的。同樣,對於CommandB打印「CommandB」的30%的時間,並在這30%打印50%的「上一頁」和打印50%的「真」 和打印50% %「新」。 進出的30%印刷電路30%, 「真」 和印刷70%的 「假」
所以目前在我下面的代碼,我打印CommandA的70%和CommandB的30%。我不知道我應該如何爲上述要求添加代碼。
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.我爲百分比執行寫的邏輯來自我在stackoverflow上發佈的帖子。
我沒有看到任何隨機性以所列代碼 – Attila
隨機性是存在在哪個命令上的重量被拾取基礎感。或者什麼是從頭開始解決這個問題的最好方法。 – ferhan