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

时间:2015-01-11 13:40:20

标签: java random

我正在尝试编写一个读取txt文件的程序,其中有4000或(x)个单词。到目前为止,我设法编写了一个你在下面看到的程序。它有点像在一个单词的randon位置添加(随机)星数(*)。不过这是问题所在。它总是添加一个*。我怎样才能改变它,以便将随机数量的星星添加到一个单词中。

例如:让我们说一下母亲。 我想要一个程序,也可以在这个单词的随机位置添加一个随机数*。在这个例子中,随机数的星数将是0,1,2,3,4,5或6。如果我多次运行程序,我会得到不同的结果,如:(*显示为?下面)

M ?? her,Mot ?? r,母亲,?其他,??????,M ???? r,等。

欢迎任何想法。

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();

    }
}
}

3 个答案:

答案 0 :(得分:0)

我会改变:

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

有关:

for (int i = 0; i < dolzina; i++) {
    int randomInt = rGenerator.nextInt(2);
    if (randomInt > 0) {
        beseda.setCharAt(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, '*');
}

答案 1 :(得分: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
}

或者您也可以使用先前生成的相同随机int更改随机位置的字符:

beseda.setCharAt(randomInt, '*');

答案 2 :(得分:0)

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

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