Java 替换随机位置上字符串中随机数的字符

Java 替换随机位置上字符串中随机数的字符,java,random,Java,Random,我试图写一个程序,读取一个txt文件,其中有4000或(x)字。到目前为止,我成功地编写了一个程序,如下所示。它有点像是在一个单词中给一个randon位置添加了(随机)个星星(*)。然而,问题就在这里。它总是添加一个*。我怎样才能改变它,让它在一个单词中添加随机数的星星呢 例如:让我们用“母亲”这个词。 我也需要一个程序,在这个单词的任意位置加上一个*的随机数。在这个例子中,随机数是0,1,2,3,4,5或6。若我多次运行这个程序,我会得到不同的结果,比如:(*如下所示) 她,妈妈,其他人,等等

我试图写一个程序,读取一个txt文件,其中有4000或(x)字。到目前为止,我成功地编写了一个程序,如下所示。它有点像是在一个单词中给一个randon位置添加了(随机)个星星(*)。然而,问题就在这里。它总是添加一个*。我怎样才能改变它,让它在一个单词中添加随机数的星星呢

例如:让我们用“母亲”这个词。 我也需要一个程序,在这个单词的任意位置加上一个*的随机数。在这个例子中,随机数是0,1,2,3,4,5或6。若我多次运行这个程序,我会得到不同的结果,比如:(*如下所示)

她,妈妈,其他人,等等

任何想法都欢迎

public class random_menjava_z_zvezdico {

public static void main(String[] args) {
    String test;
    int dolzina = 0;
    String outputFile = "random_z_zvezdico.txt";

    ArrayList<String> list = new ArrayList();


    try {

        File file = new File("4000_random_besed.txt");
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String vrstica = bufferedReader.readLine();
        while (vrstica != null) {

            list.add(vrstica);
            vrstica = bufferedReader.readLine();
            // dolzina=list.size();
            // System.out.println(dolzina);

        }

        FileWriter fileWriter = new FileWriter(outputFile);
        PrintWriter out = new PrintWriter(fileWriter);
        System.out.println("4000 besedam je bila dodana * random krat na random mestu!");
        for (int idx = 0; idx < list.size(); ++idx) {
            test = list.get(idx);
            dolzina = test.length();

            Random rGenerator = new Random();
            StringBuilder beseda = new StringBuilder(test);

            for (int i = 0; i < dolzina; ++i) {
                int randomInt = rGenerator.nextInt(dolzina);
                beseda.setCharAt(randomInt, '*');

            }
            out.println(beseda);

        }out.close();
    } catch (IOException e) {
        e.printStackTrace();

    }
}
}
public class random_menjava_z_zvezdico{
公共静态void main(字符串[]args){
串试验;
int-dolzina=0;
String outputFile=“random_z_zvezdico.txt”;
ArrayList=新建ArrayList();
试一试{
File File=新文件(“4000_random_besed.txt”);
FileReader FileReader=新的FileReader(文件);
BufferedReader BufferedReader=新的BufferedReader(文件阅读器);
字符串vrstica=bufferedReader.readLine();
while(vrstica!=null){
列表。添加(vrstica);
vrstica=bufferedReader.readLine();
//dolzina=list.size();
//系统输出打印(dolzina);
}
FileWriter FileWriter=新的FileWriter(输出文件);
PrintWriter out=新的PrintWriter(fileWriter);
System.out.println(“4000 besedam je bila dodana*random krat na random mestu!”);
对于(int idx=0;idx
我会改变:

for (int i = 0; i < dolzina; ++i) {
    int randomInt = rGenerator.nextInt(dolzina);
    beseda.setCharAt(randomInt, '*');
}
for(int i=0;i
用于:

for(int i=0;i0){
贝塞达·塞查拉特(i,“*”);
}
}
但更好的解决办法是:

StringBuilder orig = new StringBuilder("Mother");
Random rnd = new Random();

int maskSize = rnd.nextInt(orig.length()+1);

for (int i = 0; i < maskSize; i++) {
    int pos = rnd.nextInt(orig.length());

    while (orig.charAt(pos) == '*') {
        pos++;
        if (pos >= orig.length()) {
            pos = 0;
        }
    }

    orig.setCharAt(pos, '*');
}
StringBuilder orig=新的StringBuilder(“母亲”);
随机rnd=新随机();
int maskSize=rnd.nextInt(原始长度()+1);
for(int i=0;i=原始长度()){
pos=0;
}
}
原始设置字符(位置“*”);
}
然后,如果您想更改随机数目的字符

int positionCount = 0;//this wiil be used to change the character at specific location starting from the first location

while(randomInt >= 0){
//change this line to randomInt > 0 if you don't want to change anything when you have zero as randomInt

  beseda.setCharAt(positionCount, '*');//this replace the next character starting from the first

  //control the values in order for the position not to exceed the string by using if statement
  if(positionCount < beseda.toString().length){
     positionCount++;
  }

  randomInt--;//decrease the random number by one to enable escape of while loop when it is done
}

我发现您没有定义不同配置所需的概率分布。在我的解决方案中,我假设您需要一个统一的概率(即“母亲”/“*其他”/“**其他人”的概率都是1/64):

private static final Random rnd=new Random();
公共静态字符串replaceRandom(字符串s){
char[]chars=s.toCharArray();
位集bs=新位集();
字节[]字节=新字节[chars.length/8+1];
rnd.nextBytes(字节);
bs=位集。值(字节);
int i=bs.nextSetBit(0);
而(i
很好,它工作得更好,谢谢,但它仍然没有添加0星选项,我需要它添加0或更多,目前它只适用于0以上。他希望随机数介于0和6之间(含6),而不是rGenerator。下一步(2);我认为它应该是发电机;我前面的示例适用于0或更多,但0的概率非常低。我编辑了这篇文章,所以所有的“*”都有相同的概率。
int randomInt = rGenerator.nextInt(7);//this gets you a number between 0 and 6 (inclusive)
int positionCount = 0;//this wiil be used to change the character at specific location starting from the first location

while(randomInt >= 0){
//change this line to randomInt > 0 if you don't want to change anything when you have zero as randomInt

  beseda.setCharAt(positionCount, '*');//this replace the next character starting from the first

  //control the values in order for the position not to exceed the string by using if statement
  if(positionCount < beseda.toString().length){
     positionCount++;
  }

  randomInt--;//decrease the random number by one to enable escape of while loop when it is done
}
beseda.setCharAt(randomInt, '*');
  private static final Random rnd = new Random();

  public static String replaceRandom(String s) {
    char[] chars = s.toCharArray();
    BitSet bs = new BitSet();
    byte[] bytes = new byte[chars.length/8 + 1];
    rnd.nextBytes(bytes);
    bs = BitSet.valueOf(bytes);
    int i = bs.nextSetBit(0);
    while (i < chars.length && (i != -1)) {
      chars[i] = '*';
      i = bs.nextSetBit(i + 1);
    }

    return new String(chars);
  }