Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Math.Random() query

 
pallavi chaudhari
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes there is. What was your try to accomplish that?
 
pallavi chaudhari
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oke but what did you code to exclude those characters?
 
pallavi chaudhari
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48964
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48964
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48964
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And when I started writing, I didn't know you were posting too.

Agree about back to topic, please.
 
pallavi chaudhari
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once again thanks to all .

Thanks,
Pallavi
 
Gaurav Raje
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try
 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which can theoretically make the loop take hours to complete, if the random number always leads to 'I'.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12125
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaurav Raje wrote:try

I'm guessing that won't even compile, as you have mis-matched single and double quotes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic