2012-05-22 28 views
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) 

以上就是我的文本文件中,我打印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上发布的帖子。

+0

我没有看到任何随机性以所列代码 – Attila

+0

随机性是存在在哪个命令上的重量被拾取基础感。或者什么是从头开始解决这个问题的最好方法。 – ferhan

回答

1

您可以使用java.util.Random类生成随机数。 Random.nextDouble()方法返回一个介于0和1之间的值,所以如果你用100乘以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(); 
    } 
} 

:上述代码假定的所有Command小号getExecutionPercentage()总和(至少)100

更新:由Random对象作为方法不是静态

+0

感谢您的解释。你能否给我一个满足我上述要求的例子,通过这种方式我可以更多地理解并做出所需的更改。 – ferhan

+0

@RaihanJamal - 查看更新 – Attila

+0

你确定这样会执行所需的percenatges时间吗?我仍然不确定。 – ferhan