This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes Math.Random() query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Math.Random() query" Watch "Math.Random() query" New topic
Author

Math.Random() query

pallavi chaudhari
Ranch Hand

Joined: Jul 02, 2008
Posts: 54
Hi,

Is there any way to exclude 'l' and 'O' from the below statement result :

(Math.random() * 26 + 'A')

Thanks,
Pallavi
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Yes there is. What was your try to accomplish that?


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
pallavi chaudhari
Ranch Hand

Joined: Jul 02, 2008
Posts: 54
I am trying to generate random password and I want to exclude lower case 'l' and uppercase 'O' from password .

Also I wanted to remove 0 and 1 digit i did for this : (Math.random() * 8 + 2)
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Oke but what did you code to exclude those characters?
pallavi chaudhari
Ranch Hand

Joined: Jul 02, 2008
Posts: 54
I am manually checking like :

for e.g :

char c = (char)(Math.random() * 26 + 'a');
if (c == 'l')
continue;

else
pwd = pwd + c;
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Which may cause the same two, unwanted characters to be generated each time. It's unlikely but still possible.

A simple way to solve this:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
I would suggest you create an array with the desired characters in, and use the length of that array as the multiplicand for random().
pallavi chaudhari
Ranch Hand

Joined: Jul 02, 2008
Posts: 54
Thanks all for your help.

please check this :

final String[] alphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","P","Q","R","S","T","U","V","W","X","Y","Z"}; // all but and O
char index = (char)(Math.random() * alphabet.length + 'A');
pallavi chaudhari
Ranch Hand

Joined: Jul 02, 2008
Posts: 54
Rob Spoor wrote:Which may cause the same two, unwanted characters to be generated each time. It's unlikely but still possible.

A simple way to solve this:


I am not getting how it will generate unwanted characters?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Campbell Ritchie wrote:I would suggest you create an array with the desired characters in, and use the length of that array as the multiplicand for random().

Isn't that what I did, only with a String instead of a char[]?

pallavi chaudhari wrote:
Rob Spoor wrote:Which may cause the same two, unwanted characters to be generated each time. It's unlikely but still possible.

A simple way to solve this:


I am not getting how it will generate unwanted characters?

It will not, but I thought you didn't want the I and O to be generated?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Rob Spoor wrote: . . . Isn't that what I did, only with a String instead of a char[]? . . .
Yes, but I hadn't noticed you had beaten me. I only notice when you beat me by 2 seconds, not 2 whole minutes

And I was too busy yesterday evening (going to see some Shakespeare in the Sunshine ) to check this post.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

And I didn't notice you posted only 2 minutes after me. For me there were a few hours between posting and reading your post.

Anyway, back on topic.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
And when I started writing, I didn't know you were posting too.

Agree about back to topic, please.
pallavi chaudhari
Ranch Hand

Joined: Jul 02, 2008
Posts: 54
Once again thanks to all .

Thanks,
Pallavi
Gaurav Raje
Ranch Hand

Joined: Jul 23, 2010
Posts: 136
try
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Which can theoretically make the loop take hours to complete, if the random number always leads to 'I'.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11439
    
  16

Gaurav Raje wrote:try

I'm guessing that won't even compile, as you have mis-matched single and double quotes.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Math.Random() query