954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Password Generator

I am trying to create a program that generates random passwords using the characters A-Z and the numbers 0-9. It will also have an input of what you want the length of the password to be.

What I don't know how to do is to create a method to choose random, arbitrary characters/ numbers.

Are there any objects that specialize in selecting random characters?

bloody_ninja
Junior Poster in Training
96 posts since Jul 2008
Reputation Points: 9
Solved Threads: 2
 

There is a sample in .NET if you can convert it to java

http://nayyeri.net/blog/generating-random-strings-in-net/

nitinmukesh
Newbie Poster
9 posts since Jul 2008
Reputation Points: 10
Solved Threads: 2
 

I am trying to create a program that generates random passwords using the characters A-Z and the numbers 0-9. It will also have an input of what you want the length of the password to be.

What I don't know how to do is to create a method to choose random, arbitrary characters/ numbers.

Are there any objects that specialize in selecting random characters?

You may simply have to create your own.

The concept isn't too hard though.

In Ascii A is 65. I'd assume Z would be 65 + 25 (or maybe 26... too tired to think really).
Once the number is chosen do a cast to the char type.

import java.util.*;

public class Generator{

	private Random rgen = new Random();
	private byte decision, numValue;
	private char charValue;

	public static void main(String... args){
		System.out.println(new Generator().gen(9));
	}

	public String gen(int length){
		StringBuilder sb = new StringBuilder();
		while(sb.length() < length){
			decision = (byte)rgen.nextInt(2);
			numValue = (byte)rgen.nextInt(10);
			charValue = (char)(rgen.nextInt(25) + 65);
			sb.append( (decision%2 == 0) ? ( charValue + "" ) : ( numValue + "") );
		}
		return sb.toString();
	}
}
Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

String array of A-Z characters and integers 0-9, which makes 36 elements (26 letters plus 10 numbers). Create random number generator between 0-35 with "i" loop to get password of certain length. Wouldn't be that the simplest solution?

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

i have something like that

big monie
Newbie Poster
1 post since Jul 2008
Reputation Points: 10
Solved Threads: 1
 
String array of A-Z characters and integers 0-9, which makes 36 elements (26 letters plus 10 numbers). Create random number generator between 0-35 with "i" loop to get password of certain length. Wouldn't be that the simplest solution?


I was thinking about that, but I still don't know how to write line to generate a random number 0-35. I just need that one line that generates the randomness, arg.

bloody_ninja
Junior Poster in Training
96 posts since Jul 2008
Reputation Points: 9
Solved Threads: 2
 

Math.random()

The above returns a random number between 0 and 1
a pseudorandom double greater than or equal to 0.0 and less than 1.0. Math.random()

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

HOw to get random number between 0 -35

Random rand = new Random();
int num = rand.nextInt(35);
peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

Math.random()

The above returns a random number between 0 and 1

Math.random()

Yes, and since it's a random number between 0 and 1 you can use this to your advantage by multiplying the result with a number that you want to be the "range" for randomness.

Math.random() * 35 generates a number that may be >= 0 or < 35.

Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You