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

  • Home
  • SEARCH
  • 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 822097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:44:59+00:00 2026-05-15T02:44:59+00:00

I need to create a random string which should be between the length of

  • 0

I need to create a random string which should be between the length of 6 to 10 but it sometimes generates only about the length of 3 to 5.
Here’s my code. Can anyone would be able to find out the problem? 🙁

            int lengthOfName = (int)(Math.random() * 4) + 6;
        String name = "";
        /* randomly choosing a name*/
        for (int j = 0; j <= lengthOfName; j++) {
            int freq = (int)(Math.random() * 100) + 1;
            if(freq <= 6){
                name += "a";
            }if(freq == 7 && freq == 8){
                name += "b";
            }if(freq >= 9 && freq <= 11){
                name += "c";
            }if(freq >= 12 && freq <= 15){
                name += "d";
            }if(freq >= 16 && freq <= 25){
                name += "e";                        
            }if(freq == 26 && freq == 27){
                name += "f";
            }if(freq == 28 && freq == 29){
                name += "g";
            }if(freq >= 30 && freq <= 33){
                name += "h";
            }if(freq >= 34 && freq <= 48){
                name += "i";
            }if(freq == 49 && freq == 50){
                name += "j";
            }if(freq >= 51 && freq <= 55){
                name += "k";
            }if(freq >= 56 && freq <= 60){
                name += "l";
            }if(freq == 61 && freq == 62){
                name += "m";
            }if(freq >= 63 && freq <= 70){
                name += "n";
            }if(freq >= 71 && freq <= 75){
                name += "o";
            }if(freq == 76 && freq == 77){
                name += "p";
            }if(freq == 78){
                name += "q";
            }if(freq >= 79 && freq <= 84){
                name += "r";
            }if(freq == 85 && freq == 86){
                name += "s";
            }if(freq == 87 && freq == 88){
                name += "t";
            }if(freq >= 89 && freq <= 93){
                name += "u";
            }if(freq == 94){
                name += "v";
            }if(freq == 95 && freq == 96){
                name += "w";
            }if(freq == 97){
                name += "x";
            }if(freq == 98 && freq == 99){
                name += "y";
            }if(freq == 100){
                name += "z";
            }
        }
  • 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-05-15T02:45:00+00:00Added an answer on May 15, 2026 at 2:45 am

    I’m sorry but the code is too poorly written to be salvageable. I recommend something like this.

        Random r = new Random(); // just create one and keep it around
        String alphabet = "abcdefghijklmnopqrstuvwxyz";
    
        final int N = 10;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < N; i++) {
            sb.append(alphabet.charAt(r.nextInt(alphabet.length())));
        }
        String randomName = sb.toString();
    
        System.out.println(randomName);
    

    Key points are:

    • Use java.util.Random, specifically nextInt(int n) to get a random int in a given range
      • No need for funky formulas
    • When building a string in a loop, use StringBuilder.
    • Use an alphabet string, and charAt to index its letters.

    API links

    • java.util.Random
      • int nextInt(int n)
        • Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)
    • StringBuilder – A mutable sequence of characters.

    Related questions

    • String vs StringBuilder (C#)
    • String, StringBuffer and StringBuilder (Java)

    Problems with the original code

    There are plenty, unfortunately.

    • String += in a loop yields very poor performance for longer strings
    • for (int j = 0; j <= lengthOfName; j++) is an off-by-one-error
    • freq == 7 && freq == 8 is a logical contradiction
    • It’s just unnecessarily verbose!
      • Warning signs should go off whenever you write something like that

    I highly recommend doing lots of small but simple exercises to learn Java basics. codingbat.com is great; it has hundreds of these, they’re automatically graded so you’ll know when your solution works as expected or not. It has sections on logic, strings, arrays, etc.


    On uneven letter distribution

    The simplest solution is to just have duplicates in the alphabet:

    • String alphabet = "aab"; will have probability for a twice as much as b
    • You can generate the alphabet programmatically from a frequency table
      • I’ll leave this as an exercise (or you can ask another question if you need it)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a comma separated string, out of which I need to create a
I need to set up a system which will allow developers to request an
I create class libraries, some which are used by others around the world, and
I am creating a webpage which is using asp .net as its backend. I
I needed a unique internal int to use that would represent several random content
I would like to randomly iterate through a range. Each value will be visited
I don't understand how using a 'challenge token' would add any sort of prevention:
I am using codeigniter's session class to handle my PHP sessions. One of the
I want to use object instances as modules/functors, more or less as shown below:

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.