Quantcast
Channel: How to generate a random alpha-numeric string - Stack Overflow
Viewing all articles
Browse latest Browse all 49

Answer by Burak T for How to generate a random alpha-numeric string?

$
0
0

You can create a character array which includes all the letters and numbers, then you can randomly select from this char array and create your own string password.

char[] chars = new char[62]; // sum of letters and numbers

int i = 0;

    for(char c = 'a'; c <= 'z';c++) { // for letters
        chars[i++] = c;
    }

    for(char c = '0'; c <= '9';c++) { // for numbers
        chars[i++] = c;
    }

    for(char c = 'A'; c <= 'Z';c++) { // for capital letters
        chars[i++] = c;
    }

    int numberOfCodes = 0;
    String code = "";
    while (numberOfCodes < 1) {//enter how much you want to generate at one time
        int numChars = 8; //Enter how many digits you want in your password

        for(i = 0; i < numChars; i++) {
            char c = chars[(int)(Math.random() * chars.length)];
            code = code + c;
        }
        System.out.println("Code is :" + code);
    }

Viewing all articles
Browse latest Browse all 49

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>