Java 随机字母选择函数

Java 随机字母选择函数,java,Java,我正在尝试编写一个函数,根据字母的频率计数来选择字母 这里,字母a到z具有以下频率: 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 Letter chooseOnWeight(List<Let

我正在尝试编写一个函数,根据字母的频率计数来选择字母

这里,字母a到z具有以下频率:

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 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.");
}
public Letter chooseOnWeight(列表字体新建){
int completeWeight=0;
int completeWeightUpdated=0;
List updatedWeightList=新的ArrayList();
适用于(信函:letterNew){
updatedWeightList=letter.getWeight();
对于(int i=0;i=r){
回信;
}
}
抛出新的RuntimeException(“不应显示”);
}
包含getWeight()函数的Letter类:

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


}
公开课信函{
字符名;
列表权重;
公开信(字符名称){
this.name=名称;
}
公共字符getName(){
返回名称;
}
公共列表getWeight(){
返回重量;
}
公共空隙设定重量(列表重量){
重量=重量;
}
}

不幸的是,生成的字母只有a-f。出于某种原因,它不会在f之后生成任何字母

在第二个主for循环每次迭代后,将
countWeight
重置为
0
,在该循环中,您在
letterNew
中迭代每个
字母。因为通过不重置
countWeight
countWeight updated
的预期值将在内部for循环之后受到影响,在该循环中,您将
countWeight
增加
updatedCountList
中的单词长度。在这个内部for循环之后,
countWeightUpdated
应该只增加当前被迭代的
字母的总权重值,而不是到目前为止所有
字母的实例的总权重值。例如,如果a的总重量为1000,b的总重量为1100,c的总重量为1200,则在迭代a、b和c之后,
countWeightUpdated
的值仅为1000+1100+1200=3300。但是,您的循环将执行以下加法,1000+2100+3300=8400,因为每次迭代后
countWeight
将是之前查看的所有字母的总重量,而不是每个字母的重量。在这种情况下,2100来自(1000+1100),3300来自(1000+1100+1200)。因此,这会将返回的字母范围限制在较短的范围内。解决办法很简单

for (Letter letter : letterNew) {
    updatedCountList = letter.getWeight();
    for (int i = 0; i < updatedWeightList.size(); i++) {
        countWeight += updatedCountList.get(i);
    }
    countWeightUpdated += countWeight;
    countWeight = 0; //THIS IS THE FIX
    if (countWeightUpdated >= r) {
        return letter;
    }
}
for(字母:letterNew){
updatedCountList=letter.getWeight();
对于(int i=0;i=r){
回信;
}
}

另外,在第一个主for循环中,completeWeightUpdated出现了类似的问题,但是在方法中没有太多使用,所以我没有提到它,因为它似乎是多余的。

也许可以向我们展示一下你的
getWeight
方法将添加getWeight(),现在创建一个树状图,其中的关键是字母的累积重量(A为4778,B为4778+1145,C为4778+1145+1994,等等),其值为字母。此操作需要在列表上进行一次传递。生成一个介于0和Z累积权重之间的随机数,并调用map.ceilingEntry()查找与此随机数对应的字母。您的
chooseOnWeight
方法中是否缺少任何内容?因为我看不到您在其中返回任何内容,它返回字母,就在if(countWeightUpdated>=r)下面