I was curious if there were methods in java to randomly generate random numbers and uppercase and lowercase letters. i am creating a password generator for a project. i am still a little new to java also.
Asked
Active
Viewed 3,767 times
-7
-
I believe the answer is no. :) – Kick Buttowski Oct 20 '14 at 06:37
-
2"I was curious if there were methods" - so why didn't you use Google or the Java documentation then? – The Paramagnetic Croissant Oct 20 '14 at 06:38
-
1possible duplicate of [How to generate a random alpha-numeric string?](http://stackoverflow.com/questions/41107/how-to-generate-a-random-alpha-numeric-string) – Baby Oct 20 '14 at 06:42
-
1If people say that there is a class called [Random](http://docs.oracle.com/javase/6/docs/api/java/util/Random.html) in java, they are lying. :) – TheLostMind Oct 20 '14 at 06:42
2 Answers
0
import java.util.Random;
Code is
Random r = new Random();
Integer num = 0;
for(int i=0;i<100;i++){
num=r.nextInt(20000);
System.out.println(num);
}
Output : 12228 8875 10340 8898 11949 8184 14417 17284

Shaik Md
- 597
- 4
- 8
- 22
-
i guess what OP stated is _"..to randomly generate random numbers and uppercase and lowercase letters"_ – Baby Oct 20 '14 at 07:01
0
Also you can use Math.random() for generating random number. To generate a random letter you need to generate a number between 0 and 26 and then add it to the char number of 'A' or 'a'. For example
int firstUppercaseIndex = (int)'A'; // for uppercase
int firstLowercaseIndex = (int)'a'; // for lowercase
for (int i = 0; i < 10; i++) {
Random r = new Random();
int letterIndex = r.nextInt(26); // random number between 0 and 26
char randomLowercase = (char) (firstLowercaseIndex + letterIndex);
char randomUppercase = (char) (firstUppercaseIndex + letterIndex);
System.out.println("randomLowercase = " + randomLowercase);
System.out.println("randomUppercase = " + randomUppercase);
}

mehdinf
- 59
- 4