Programme Java pour créer des chaînes aléatoires

Dans cet exemple, nous allons apprendre à générer une chaîne aléatoire et une chaîne aléatoire alphanumérique en Java.

Pour comprendre cet exemple, vous devez avoir la connaissance des rubriques de programmation Java suivantes:

  • Chaîne Java
  • Java pour Loop
  • Java String charAt ()

Exemple 1: programme Java pour générer une chaîne aléatoire

 import java.util.Random; class Main ( public static void main(String() args) ( // create a string of all characters String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // create random string builder StringBuilder sb = new StringBuilder(); // create an object of Random class Random random = new Random(); // specify length of random string int length = 7; for(int i = 0; i < length; i++) ( // generate random index number int index = random.nextInt(alphabet.length()); // get character specified by index // from the string char randomChar = alphabet.charAt(index); // append the character to string builder sb.append(randomChar); ) String randomString = sb.toString(); System.out.println("Random String is: " + randomString); ) )

Production

 La chaîne aléatoire est: IIYOBRK

Dans l'exemple ci-dessus, nous avons d'abord créé une chaîne contenant tous les alphabets. Ensuite, nous avons généré un numéro d'index aléatoire en utilisant la nextInt()méthode de la Randomclasse.

En utilisant le numéro d'index aléatoire, nous avons généré le caractère aléatoire à partir de l'alphabet de chaîne. Nous avons ensuite utilisé la StringBuilderclasse pour ajouter tous les caractères ensemble.

Si nous voulons changer la chaîne aléatoire en minuscules, nous pouvons utiliser la toLowerCase()méthode du String.

 randomString.toLowerCase()

Remarque : la sortie sera différente à chaque fois que vous exécutez le programme.

Exemple 2: programme Java pour générer une chaîne alphanumérique aléatoire

 import java.util.Random; class Main ( public static void main(String() args) ( // create a string of uppercase and lowercase characters and numbers String upperAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; String lowerAlphabet = "abcdefghijklmnopqrstuvwxyz"; String numbers = "0123456789"; // combine all strings String alphaNumeric = upperAlphabet + lowerAlphabet + numbers; // create random string builder StringBuilder sb = new StringBuilder(); // create an object of Random class Random random = new Random(); // specify length of random string int length = 10; for(int i = 0; i < length; i++) ( // generate random index number int index = random.nextInt(alphaNumeric.length()); // get character specified by index // from the string char randomChar = alphaNumeric.charAt(index); // append the character to string builder sb.append(randomChar); ) String randomString = sb.toString(); System.out.println("Random String is: " + randomString); ) )

Production

 La chaîne alphanumérique aléatoire est: pxg1Uzz9Ju

Ici, nous avons créé une chaîne contenant les nombres de 0 à 9 et les alphabets en majuscules et minuscules.

À partir de la chaîne, nous avons généré au hasard une chaîne alphanumérique de longueur 10 .

Articles intéressants...