How to generate the random alphanumeric string in Java
In this post we will give you information about How to generate the random alphanumeric string in Java. Hear we will give you detail about How to generate the random alphanumeric string in JavaAnd how to use it also give you demo for it if it is necessary.
This below example will help you to generate a random alphanumeric string of the specific length inby using the Math.random() method in Java.
// Java programpublic class RandomString { static String getAlphaNumericString(int n) { String AlphaNumericString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789" + "abcdefghijklmnopqrstuvxyz"; StringBuilder sb = new StringBuilder(n); for (int i = 0; i < n; i++) { int index = (int)(AlphaNumericString.length()* Math.random()); sb.append(AlphaNumericString.charAt(index)); } return sb.toString(); } public static void main(String[] args) { // printing random string of length 12 System.out.println(RandomString.getAlphaNumericString(12)); }}
Output:
tdiT7EqrWAL7
Hope this code and post will helped you for implement How to generate the random alphanumeric string in Java. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs