在随机位置替换字符串中的随机字符数
Replacing random number of characters in a string on random places
我正在尝试编写一个程序来读取一个包含 4000 或 (x) 个单词的 txt 文件。到目前为止,我设法编写了一个程序,如下所示。它有点像将(随机)星数(*)添加到单词中的随机位置。然而,问题就在这里。它总是添加一个 *。我该如何更改它,以便它可以为单词添加随机数量的星星。
例如:让我们以母亲这个词为例。
我想要一个程序也将随机数 * 添加到此 word.That 中的随机位置,在此示例中,随机数的星星将是 0、1、2、3、4、5 或 6。如果我 运行 多次编程,我会得到不同的结果,例如:(* 在下面显示为 ?)
M??her , Mot??r, Mother, ?other, ??????, M????r ,ect.
欢迎提出任何想法。
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();
}
}
}
我会改变:
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, '*');
}
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, '*');
我看到您没有定义不同配置所需的概率分布。我在我的解决方案中假设你需要一个统一的概率(即 "mother"/"*other"/"**ther" 的概率都是 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);
}
我正在尝试编写一个程序来读取一个包含 4000 或 (x) 个单词的 txt 文件。到目前为止,我设法编写了一个程序,如下所示。它有点像将(随机)星数(*)添加到单词中的随机位置。然而,问题就在这里。它总是添加一个 *。我该如何更改它,以便它可以为单词添加随机数量的星星。
例如:让我们以母亲这个词为例。 我想要一个程序也将随机数 * 添加到此 word.That 中的随机位置,在此示例中,随机数的星星将是 0、1、2、3、4、5 或 6。如果我 运行 多次编程,我会得到不同的结果,例如:(* 在下面显示为 ?)
M??her , Mot??r, Mother, ?other, ??????, M????r ,ect.
欢迎提出任何想法。
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();
}
}
}
我会改变:
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, '*');
}
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, '*');
我看到您没有定义不同配置所需的概率分布。我在我的解决方案中假设你需要一个统一的概率(即 "mother"/"*other"/"**ther" 的概率都是 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);
}