2012-05-15 30 views
2
Task1, X% 
Task2, Y% 
Task3, Z% 
---- 
TaskN, N% 

And X + Y + Z + . . . + N = 100% 

問題: -上述配置文件有某種類型的稱重任務不同的(執行時間任務1 X%,並打印在1號線什麼,執行任務2 Y%的時間和2號線打印出任何東西,執行的時間任務3ž%,與3號線打印任何東西,等等等等,總重應爲100%,我寫調用不同的任務根據他們的百分比

代碼: -我寫的一個程序,我正在逐行讀取文件並存儲所有行Ñ字符串的ArrayList中,然後我生成1和100之間的一些隨機數因此,例如,有三個任務 -

Task1 75% 
Task2 15% 
Task3 10% 

獲取1-100之間的隨機數。如果數字是1-75,則任務1意味着在行1上打印任何東西,76-90任務2意味着在行2上打印任何東西,91-100是任意3意味着在行3上打印任何東西。但是我怎樣才能擴展這個任何具有N個任務百分比數的配置文件。我被困在這一部分。

   private static Random r = new Random(); 
       private final static int Low = 1; 
       private final static int High = 100; 
       String sCurrentLine; 
       ArrayList<Integer> percentageCalls = new ArrayList<Integer>(); 

       br = new BufferedReader(new FileReader("S:\\config.txt")); 

       int R = r.nextInt(High-Low) + Low; 

       ArrayList<List<String>> allLines = new ArrayList<List<String>>(); 
       while ((sCurrentLine = br.readLine()) != null) { 
        allLines.add(Arrays.asList(sCurrentLine.split("\\s+"))); 
        Collections.shuffle(allLines); 
       } 

       for (List<String> s1 : allLines) { 
        percentageCalls.add(Integer.parseInt(s1.get(0))); 
       } 
       Collections.sort(percentageCalls, Collections.reverseOrder()); 
+0

這是相同的問題(也非常近的)作爲[鏈接](http://stackoverflow.com/questions/10607614/percentage -of-隨機調用到不同的命令,在文本文件)。我認爲問題/解決方案的整個概念是有缺陷的,但請在這裏尋找一些答案。 – user949300

+0

我剛剛看到了這個鏈接,但無法確定如何將它擴展到N個Task。 – AKIWEB

+0

交叉發表:[基於不同任務的調用百分比](http://www.java-forums.org/advanced-java/59778-call-different-task-based-their-percentage.html ) –

回答

2

我最近寫了一些東西來做這個項目,你應該可以適應。該類允許將任意數量的選項添加到「可能性」列表中。機會數量可以是任何整數......總數不需要累加到100.所以如果你想要選擇A在302次中出現5次而選擇B在其他297次中發生,那麼你可以將A與一個5和B的機會有297的機會。這個代碼有點粗糙,可能需要一些改進,但這個想法應該可以幫助你擴展到N個選擇。你只需要寫一些循環遍歷文件,並將每種可能性添加到R​​esultGenerator。

樣品使用:

ResultGenerator<String> rgen = new ResultGenerator<String>(); 

rgen.AddPossibility("Red", 25); 
rgen.AddPossibility("Blue", 20); 
rgen.AddPossibility("Green", 30); 
rgen.AddPossibility("Black", 5); 
rgen.AddPossibility("White", 15); 

String result = rgen.GetRandomResult(); 

類別:

public class ResultGenerator<T> { 

    class Possibility 
    { 
     Possibility(T choice, int chance) 
     { 
      this.Choice = choice; 
      this.Chance = chance; 
     } 

     public T Choice; 
     public int Chance; 
     public int RangeMax; 
     public int RangeMin; 

    } 

    private Random r; 

    public ResultGenerator(Random r) 
    { 
     this.r = r; 
    } 

    public ResultGenerator() 
    { 
     this.r = new Random(); 
    } 

    private List<Possibility> possibilities = new ArrayList<Possibility>(); 

    public void AddPossibility(T choice, int chance) { 
     possibilities.add(new Possibility(choice, chance)); 
    } 

    public T GetRandomResult() { 

     if (possibilities.size() == 0) 
      return null; 

     //Calculate ranges for possibilities 
     int totalChances = 0; 
     for (Possibility p : possibilities) { 
      p.RangeMin = totalChances; 
      p.RangeMax = totalChances + p.Chance; 
      totalChances += p.Chance; 
     } 

     int randomNumber = 1 + r.nextInt(totalChances + 1); 

     for(Possibility possibility : possibilities) 
     { 
      if (randomNumber <= possibility.RangeMax && randomNumber > possibility.RangeMin) 
      { 
       return possibility.Choice; 
      } 
     } 

     return null; 
    } 

}