How To Generate Random String Without Duplicate Values In Java Android

Random string is needed when testing some variable in java. Random String without duplicate values will help vaeriable more valid while testing. This code below will string and put them in TextView widget.

   public static String getRandomString(int length) {  
     StringBuilder result = new StringBuilder();  
     String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";  
     Random rand = new Random();  
     while(length > 0) {  
       String h = String.valueOf(characters.charAt(rand.nextInt(characters.length())));  
       if(!result.toString().contains(h)){  
            result.append(h);  
            length--;  
       }  
       Log.d ("yy",characters);  
     }  
     return result.toString();  
    }  

This java code below is how to use that function

     textView.setText(getRandomString(20));  

Comments

Popular posts from this blog

Tutorial Integration Firebase With Admob on Android

Firebase Configuration in Android

How To Generate A Random String Alphabet And Number in Java Android