• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Math.Random() query  RSS feed

 
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
 
Bartender
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
Bartender
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;
 
Sheriff
Posts: 21459
96
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:
 
Marshal
Posts: 60130
188
  • 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
Posts: 21459
96
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
Marshal
Posts: 60130
188
  • 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
Posts: 21459
96
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
Marshal
Posts: 60130
188
  • 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
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try
 
Rob Spoor
Sheriff
Posts: 21459
96
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'.
 
lowercase baba
Bartender
Posts: 12627
50
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!