# Math.Random() query

pallavi chaudhari
Ranch Hand
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
Posts: 2700
Yes there is. What was your try to accomplish that?

pallavi chaudhari
Ranch Hand
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
Posts: 2700
Oke but what did you code to exclude those characters?

pallavi chaudhari
Ranch Hand
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
Posts: 20545
56
• 1
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:

Campbell Ritchie
Sheriff
Posts: 49367
62
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
Posts: 54

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
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
Posts: 20545
56
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
Posts: 49367
62
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
Posts: 20545
56
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
Posts: 49367
62
And when I started writing, I didn't know you were posting too.

pallavi chaudhari
Ranch Hand
Posts: 54
Once again thanks to all .

Thanks,
Pallavi

Gaurav Raje
Ranch Hand
Posts: 136
try

Rob Spoor
Sheriff
Posts: 20545
56
Which can theoretically make the loop take hours to complete, if the random number always leads to 'I'.

fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Gaurav Raje wrote:try

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

 It is sorta covered in the JavaRanch Style Guide.