Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7996115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:37:24+00:00 2026-06-04T14:37:24+00:00

Generate a random string of any particular length. I know this question has been

  • 0

Generate a random string of any particular length. I know this question has been asked many times, I wrote this code below, I just wanted to know is there any better approach then the below code I wrote? Or we can make the below code more efficient?

public static void main(String[] args) {
    String s = randomString(25);
    System.out.println(s);
}

public static String randomString(final int length) {
    StringBuilder sb = new StringBuilder();
    Random r = new Random();
    String subset = "0123456789abcdefghijklmnopqrstuvwxyz";
    for (int i = 0; i < length; i++) {
        int index = r.nextInt(subset.length());
        char c = subset.charAt( index );
        sb.append( c );
    }
    return sb.toString();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:37:26+00:00Added an answer on June 4, 2026 at 2:37 pm

    Since you know the length ahead of time, setup the StringBuilder with a capacity:

    StringBuilder sb = new StringBuilder(length);

    This alone eliminates the unnecessary resizing of the internal array within the StringBuilder.

    That being said, you’re probably better off using a char[] array instead of StringBuilder, and just represent subset as a char[] as well.

    private static final char [] subset = "0123456789abcdefghijklmnopqrstuvwxyz".toCharArray();
    
    char buf[] = new char[length];
    for (int i=0;i<buf.length;i++) {
      int index = r.nextInt(subset.length);
      buf[i] = subset[index];
    }
    
    return new String(buf);
    

    There’s some subtle gains to be had here by avoiding some function call overhead to “charAt” and “append”. As well as eliminating some of the memory and allocation time overhead for the StringBuilder. In general, if you know the size of a string you are building, it’s a bit more efficient to work directly with the char arrays.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to generate a random number string of N length with this
Possible Duplicate: Is this a good way to generate a string of random characters?
Does anybody know of a good way to generate a random String of specified
I used to generate random string in the following way (now I've switched to
How can I generate a random 8 character alphanumeric string in C#?
I need to generate random string for my automated tests. I use the following
Site here. Basically the box in the middle doesn't generate random string from my
I know there have been several posts about random word generation based on large
I want to generate some random IP Address. But evertime this generateIPAddress function returns
Does anyone know the name of a codec that can translate any random assortment

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.