File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Java generating password Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java generating password" Watch "Java generating password" New topic
Author

Java generating password

Jay Mize
Ranch Hand

Joined: Dec 23, 2012
Posts: 47
So I'm trying to create a password generator, this is part of my code. This part of the code basically say that if the user pick "1", the generator would generate a password base on the user input of length "passLength", now for the next part, I need to put lowercase and uppercase random letter for option "2". Uppercase letter are from 65 to 90 ( in ASCII Code) and lowercase are from 97 to 122. How do I make it so the random method generate both ASCII from 65-90 and from 97 to 122? Do I just put int randomLower = (int) ((Math.random()*(25)+(97)) && ((Math.random()*(25)+(65)))? << I try this but it don't seem to be compiling. Thanks



***If my question is confusing, what I am trying to ask is how do I make Java generate random ASCII of only lowercase and upper case letter ( A-Z and a-z)
Kathleen Angeles
Ranch Hand

Joined: Aug 06, 2012
Posts: 122

it don't seem to be compiling.


What is the compilation error message you got?
Jay Mize
Ranch Hand

Joined: Dec 23, 2012
Posts: 47
I have no idea how to make the code to generate both lowercase and uppcase letter. Instead of putting int randomLower = (int)((Math.random()*(25))+(97)); , I try to put (int) ((Math.random()*(25)+(97)) && ((Math.random()*(25)+(65))) but I that just my guess but when I put it into the code, it just say ")" missing even though I put one. In all, I have no idea how to generate random ASCII of uppercase and lowercase letter.

Kathleen Angeles
Ranch Hand

Joined: Aug 06, 2012
Posts: 122

You can post your complete code, and below that, the exact complete compiler error message (cut it and paste here)? This way, we will know what broke your code.
Jay Mize
Ranch Hand

Joined: Dec 23, 2012
Posts: 47
Oh sorry for not being clear. I'm just asking how can I generate random uppercase and lowercase letter without using array. I just wrote this part of the code so I can explain easier. Thanks
Aj Prieto
Ranch Hand

Joined: Sep 28, 2012
Posts: 72

The && is used only in boolean statements, what you have is an int.

Try:


Da mihi sis bubulae frustum assae, solana tuberosa in modo Gallico fricta ac quassum lactatum coagulatum crassum.
Jay Mize
Ranch Hand

Joined: Dec 23, 2012
Posts: 47
Won't that create an array? Is there anyway to make Java generate a number between two set of range? Like generating a mumber from 65 to 90 or 97 to 122.
Phil English
Ranch Hand

Joined: Jun 18, 2012
Posts: 62

Jay Mize wrote:Won't that create an array? Is there anyway to make Java generate a number between two set of range? Like generating a mumber from 65 to 90 or 97 to 122.


Why not generate a number between 65 and 122 and just reject it and run it again if it is between 90 and 97

Hint:


It's kind of inelegant and using an array of legitimate char values might be more simple in the end.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

you could also generate a single random number between 65 and 90. Generate a second random number that is either 0 or 1. If it is 1, add 32 to the first number, otherwise don't.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2343
    
  28

I would just have a big ass string (yes big ass is the technical term here) of allowable characters, then randomly pick an index. Something like this



This way, you can change the allowable characters easily.
Jay Mize
Ranch Hand

Joined: Dec 23, 2012
Posts: 47
Hey Phil, won't I have to force the while loop (randomLowUp > 90 && randomLowUp < 97) to start by initializing randomLowUp to a value like 92? Thanks
Jay Mize
Ranch Hand

Joined: Dec 23, 2012
Posts: 47
Jayesh A Lalwani wrote:I would just have a big ass string (yes big ass is the technical term here) of allowable characters, then randomly pick an index. Something like this



This way, you can change the allowable characters easily.


I was thinking of such method but won't this be a hassle method once you're require to have more ASCII included? Like numbers and punctuations. Thanks
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2343
    
  28

You just change it to



It takes like 2 secs
Jay Mize
Ranch Hand

Joined: Dec 23, 2012
Posts: 47
Jayesh A Lalwani wrote:You just change it to



It takes like 2 secs


True, thanks for the suggestion, it just I haven't learn.charAt and trying to understand how to do with loop and Math.random().
Phil English
Ranch Hand

Joined: Jun 18, 2012
Posts: 62

Jay Mize wrote:Hey Phil, won't I have to force the while loop (randomLowUp > 90 && randomLowUp < 97) to start by initializing randomLowUp to a value like 92? Thanks


No, just initialize your random number by calling the random generator once prior to the loop. If the number meets your criteria then it you will never enter the loop.

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7713
    
  20

Jay Mize wrote:True, thanks for the suggestion, it just I haven't learn.charAt and trying to understand how to do with loop and Math.random().

Well, Jayesh's suggestion looks like it uses java.util.Random (←click) instead, which is what I would also advise.

Math.random() is old hat, and actually uses Random under the hood now anyway.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java generating password