How to generate random characters and letters in JAVA

Posted on 2009-12-26
Last Modified: 2013-12-14

I have the following function

which generates alphabetic letter + dot so output looks like this B.

I need it to generate 5 random letters + 2 random numbers + dot

how can i achieve this without altering the code too much?
private String fnam1 = (char)((int)'A'+Math.random()*((int)'Z'-(int)'A'+1))+ ".";

Open in new window

Question by:Maranellas
    LVL 6

    Accepted Solution

    You need to get random characters five times and get random number of 2 digit length and then the 5 random characters with 2 digit number and dot.
    if the the number of character and number and the order is not mandatory, you can try below one

    UUID uuid = UUID.randomUUID();
    String myRandom = uuid.toString();
    LVL 9

    Expert Comment

    Try this:

                Random rnd = new Random();
                StringBuffer buffer = new StringBuffer();
                for (int i = 0; i < 8; i++) {
                      buffer.append(Character.toString(i < 5 ? ((char) ('A' + rnd.nextInt('Z' - 'A' + 1))) : ((i < 7) ? (char) ('0' + rnd.nextInt(9)) : '.')));

                String fnam1 = buffer.toString();

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    The viewer will learn how to implement Singleton Design Pattern in Java.
    THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now