Random String Generator Java


This video shows one way of many ways to construct a random string in Java. The concepts in this video are taken from the first few videos in my “Java Tutorials …


Xem Thêm Bài Viết Về Mobile Khác: https://sealsystem.org/mobile

Nguồn: https://sealsystem.org/


  1. can we generate all possible combination throw random.
    you are printing only one combination.
    but i want to print all possible combination. like(ABCD, ABCE,ACED…………..) SO ON.

  2. Good one mate! I tried like this and it worked too. Hope it's useful to anyone.
    public static String stringGenerator() {

    String characters = "abcdefghijklmnopqrstuvwxyz";

    String randomString = "";

    int length = 5;

    Random rand = new Random();

    for (int i = 0; i < length; i++) {

    randomString = randomString + characters.charAt(rand.nextInt(characters.length()));


    return randomString;


  3. The modified version of your code generates a random string from 0 – 15.
    How would you go about it, if you wanted your code to generate a random string that has a character length of 14 -19?

  4. I created a Random Password Generotor :
    i guess this is more simple….
    compile and run it :-

    import java.util.Random;

    import java.util.Scanner;

    public class passy {

    public static void main(String[] args) {

    Scanner s = new Scanner(System.in);

    System.out.println("Enter the lenght of your password: ");

    int len = s.nextInt();


    Random r = new Random();

    String [] array = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","1","2","3","4","5"



    int num = 0;

    String rand = "";

    for(int i=0; i<len ;i++) {

    num = r.nextInt(array.length);

    rand += array[num];


    System.out.println("Your Password is : "+rand);



  5. I had problems with the initialize of the variable i. I use a text editor and the prompt command at linux and it says that cannot find the variable i. I've write it like it was in the video. What is the matter where?? I appreciate your help, thx!!!

  6. Because String objects are immutable, each concatenation is going to create a whole new String, which is a bad habit to build if you have any intention of doing anything large-scale. There is a String(char[]) constructor, which can do what that second loop does rather more cleanly.

    It’s also a waste of time to create a random string of zero length, so line 15 in your modified version should be
    int length = rand.nextInt(14) + 1;
    , which will produce a random string of length 0<length<15.

  7. public class generate {

    * @param args the command line arguments
    public static void main(String[] args) {
    // TODO code application logic here

    String characters="abcdefghijklmnopqrstuvwxyz!#$%^&*()-=+|`ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";

    String randomString="";

    int length=5;

    Random rand=new Random();

    char[] text =new char[length];

    for (int i = 0; i < length ; i++) {

    for (int i = 0; i < text.length; i++) {
    randomString +=text[i];


  8. thank you so much for the video andrew ! i'm sorry to ask you again. i've got one more question, if i got an array of string like this :

    String [ ] symbols = {"heart", "diamond", "clubs", "spades"};

    how can i random the values of the array ? i've tried to loop the values and then stored it on an array list, but still i've got incompatible types message. here's my code :

    String [] symbol = {"heart" , "diamond" , "clubs" , "spades"};
    Random rand = new Random();
    ArrayList <String> store = new ArrayList <String> ();

    for(int i = 0; i < symbol.length; i++ ){

    could you help for this ? sorry to make trouble andrew, thank you so much for the tutorial once again :))


Please enter your comment!
Please enter your name here