Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (6.1k points)

I want to create a random, 17 characters long, ID. I am giving an example over here "AJB53JHS232ERO0H1". There should be random ordering. I tried creating an array with letters A-Z and a 'check' variable that randoms to 1-2. And in a loop; 

Randomize 'check' to 1-2.

If (check == 1) then the character is a letter.

Pick a random index from the letters array.

else

Pick a random number.

Can I do it in an easier method?

1 Answer

0 votes
by (11.7k points)

Try out this solution given down below:

protected String getSaltString() {

        String SALTCHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";

        StringBuilder salt = new StringBuilder();

        Random rnd = new Random();

        while (salt.length() < 18) { // length of the random string.

            int index = (int) (rnd.nextFloat() * SALTCHARS.length());

            salt.append(SALTCHARS.charAt(index));

        }

        String saltStr = salt.toString();

        return saltStr;

    }

By using this code above, you can generate a salt string for login purposes.

f you want to get more insights into SQL, check out this SQL Course from Intellipaat.

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...