私は頻度カウントに基づいて文字を選択する関数を書こうとしています。ここでランダムな文字の選択機能
、a〜zの文字以下の周波数がありますgetWeightが含まれている文字クラスを、
public Letter chooseOnWeight(List<Letter> letterNew) {
int completeWeight = 0;
int completeWeightUpdated = 0;
List<Integer> updatedWeightList = new ArrayList<>();
for (Letter letter : letterNew) {
updatedWeightList = letter.getWeight();
for (int i = 0; i < updatedWeightList.size(); i++) {
completeWeight += updatedWeightList.get(i);
}
completeWeightUpdated += completeWeight;
}
int countWeightUpdated = 0;
List<Integer> updatedCountList = new ArrayList<>();
double r = Math.random() * completeWeight;
double countWeight = 0.0;
for (Letter letter : letterNew) {
updatedCountList = letter.getWeight();
for (int i = 0; i < updatedWeightList.size(); i++) {
countWeight += updatedCountList.get(i);
}
countWeightUpdated += countWeight;
if (countWeightUpdated >= r) {
return letter;
}
}
throw new RuntimeException("Should never be shown.");
}
:
4778 ,1145 ,1994 ,2075 ,5940 ,762 ,1403 ,1446 ,4263 ,111 ,745 ,3231 ,1625 ,3467 ,3543 ,1455 ,94 ,3678 ,3775 ,3092 ,1883 ,529 ,649 ,139 ,902 ,180
その重み付き確率に基づいてランダムな文字を生成する関数を()関数:
public class Letter {
char name;
List<Integer> weight;
public Letter(char name){
this.name = name;
}
public char getName() {
return name;
}
public List<Integer> getWeight() {
return weight;
}
public void setWeight(List<Integer> weight) {
this.weight = weight;
}
}
残念ながら、生成されるのはa-fだけです。何らかの理由で、fの後ろに文字を生成していません。
おそらくあなた 'getWeight'方法 –
はgetWeightを(追加する私たちを見る) – statsguyz
、BのためにA、+ 1145 4778のための4778のキーは、文字の累積重量であるのTreeMap <整数、文字>、(4778作成+ 1145 + 1994 for Cなど)、値は文字です。このためには、リストの1回のパスが必要です。 0と累積加重Zの間の乱数を生成し、この乱数に対応する文字を見つけるためにmap.ceilingEntry()を呼び出します。 –