How to generates random letters from a string in Java

1 Answer

0 votes
import java.util.Random;
 
public class MyClass {
    public static void main(String args[]) {
        String s = "abcXYZ";
         
        Random r = new Random();
        for (int i = 0; i < 20; i++) {
            System.out.print(s.charAt(r.nextInt(s.length())));
        } 
    }
}
 
 
/*
run:
 
YaXYbYXZXYaZacbYcZXY
 
*/

 



answered Jul 17, 2019 by avibootz
edited Jul 18, 2019 by avibootz

Related questions

1 answer 137 views
1 answer 191 views
2 answers 240 views
1 answer 170 views
1 answer 186 views
...