Hey there! any question in your mind? Ask It Now!.

Popular Categories





random string generator function in java

+3 votes
1,829 views
asked in Programming by krish Expert (6,310 points)
want a random string generator function in java.

thanks for answer!

2 Answers

+3 votes
answered by John Tate (510 points)
selected by krish
 
Best answer
The best solution to a problem is usually the easiest one.
class randomstring {
        public static String genString(int length) {
                String string = "";
                java.util.Random random = new java.util.Random();
                int r;
                char c;
                for (int i=0; i < length; i++) {
                        r = random.nextInt(51); //base 52 random (both cases)
                        r++;
                        if (r <= 26) r = r + 64;
                        else if (r > 26) r = r + 70;
                        c = (char) r;
                        string = string + c;
                }
                return string;
        }
 
        public static void main(String args[]) {
                System.out.println(genString(24));
        }
}
 
commented by krish Expert (6,310 points)
Thanks @john its working . and i have modified System.out.println(genString(24));
to System.out.println(genString(5)); Because i need only 5 digitizes random number.
:)
commented by duke Expert (5,813 points)
@John you are right this is the right way, for a random string generated by java function, I used the same logic which you have written here. Thanks for sharing the information.
–1 vote
answered by mHelpMe (670 points)

This Function will give random string every time

public boolean fncGenerateRandString(Keyword objKeyword){
java.util.Random r = new java.util.Random();
    int i = 1, n = 0;
    char c;
    String str="Z";
    for (int t = 0; t < 3; t++) {
        while (true) {
            i = r.nextInt(10);
            if (i > 5 && i < 10) {
                if (i == 9) {
                    i = 90;
                    n = 90;
                    break;
                }
                if (i != 90) {
                    n = i * 10 + r.nextInt(10);
                    while (n < 65) {
                        n = i * 10 + r.nextInt(10);
                    }
                }
                break;
            }
        }
        c=(char)n;
        str= str + String.valueOf(c);
    }
    while(true){
    i = r.nextInt(10000000);
    if(i>999999)
        break;
    }
    str=str+i;
    System.out.println( str);
Enjoy!
commented by John Tate (510 points)
This answer doesn't even make sense!

Related Questions

+1 vote
1 answer 81 views
0 votes
0 answers 137 views
asked in Programming by vimal jain
+1 vote
1 answer 765 views
+3 votes
1 answer 626 views
+2 votes
2 answers 788 views
–1 vote
0 answers 109 views
+2 votes
1 answer 486 views
+7 votes
1 answer 566 views
asked in Programming by neha (560 points)
0 votes
0 answers 71 views
0 votes
1 answer 114 views

Not a Member yet?

Ask to Folks Login

My Account
708 Folks are online
49 members and 659 guest online
Your feedback is highly appreciated