What I would like to do is produce n number of Strings and each string should have 2 char and 4 numbers randomly shuffled, for ex: 2w3e45 34rt56 qw5498 0126fd w5487t
and so on, I have the following, it is working, any enhancements could be applied? Also, can we have them as unique strings?
import java.util.Random;
public class RandomNumAndChar {
public static void main(String[] args) {
for (int j = 0; j < 10; j++) {
Random r = new Random();
Integer[] rn = new Integer[4];
String numbers = new String();
for (int i = 0; i < rn.length; i++) {
rn[i] = r.nextInt(10);
numbers = numbers + rn[i].toString();
}
char c = (char) (r.nextInt(26) + 'a');
char c2 = (char) (r.nextInt(26) + 'a');
String chars = "" + c + c2;
String fin = numbers.concat(chars);
System.out.println(RandomNumAndChar.shuffle(fin));
}
}
public static String shuffle(String s) {
char[] characters = s.toCharArray();
for (int i = 0; i < characters.length; i++) {
int randomIndex = (int)(Math.random() * characters.length);
char temp = characters[i];
characters[i] = characters[randomIndex];
characters[randomIndex] = temp;
}
return new String(characters);
}
}
Aucun commentaire:
Enregistrer un commentaire