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 932959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:45:16+00:00 2026-05-15T20:45:16+00:00

I have some random string with unknown content, what is known is that the

  • 0

I have some random string with unknown content, what is known is that the content is alphanumeric and in lower case.

I am looking for a simple method to upper case a random number of the alpha characters in that string. The higher the randomness the better.

I can think of a few ways to do this, but none of them seem very optimal.

alright first solution:

public String randomizeCase(String myString){
  Random rand = new Random();
  StringBuilder build = new StringBuilder();
  for(char c: myString.toCharArray()){
     String s = new String(c);
     if(Character.isLetter(c) && rand.nextBoolean()){
        s = s.toUpperCase();
     } 
     build.append(s);
  }
  return build.toString();
}

I dont like this solution because:

  • 50% chance that every char is uppercased does not equal 50% chance that 50% of the chars are uppercased
  • There is a chance that nothing is upped cased
  • char to string conversion is ugly
  • 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-15T20:45:17+00:00Added an answer on May 15, 2026 at 8:45 pm

    Here is the code snippet for random sample problem (thanks Eyal for naming it). Not sure if that is what you are looking for.

    Be aware, that this solution would run into an infinete loop if not enough lowercase letters are in the string. So you would need to tackle that as well, but I guess it is a starting point. 😉

    String myString = "9aie3ra3nr23rr5r21t";
    System.out.println(upperCaseRandom(myString, 10));
    
    
    public static String upperCaseRandom(String input, int n) {
     StringBuilder output = new StringBuilder(input);
     Random r = new Random();
    
     for (int i = 0; i < n; i++) {
      // Pick a place
      int position = r.nextInt(input.length());
    
      // Check if lowercase alpha
      if (Character.isLowerCase(output.charAt(position))) {
       output.setCharAt(position, Character.toUpperCase(output.charAt(position)));
      } else {
       i--;
      } 
     } 
     return output.toString();
    }
    

    Edit:
    Here is an improved version. It does change exactly n lowercase letters into uppercase letters (if there are enough, otherwise it changes all of them). The programm does not run into infinite loops, but still running time is a problem though.

    public static String upperCaseRandom(String input, int n) {
        final int length = input.length();
        final StringBuilder output = new StringBuilder(input);
        final boolean[] alreadyChecked = new boolean[length];
        final Random r = new Random();
    
        for (int i = 0, checks = 0; i < n && checks < length; i++) {
            // Pick a place
            int position = r.nextInt(length);
    
            // Check if lowercase alpha
            if (!alreadyChecked[position]) {
                if (Character.isLowerCase(output.charAt(position))) {
                    output.setCharAt(position, Character.toUpperCase(output.charAt(position)));
                } else {
                    i--;
                }
                checks++;
                alreadyChecked[position] = true;
            } else {
                i--;
            }
        }
        return output.toString();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.