• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Math.Random() query

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22150
117
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 72619
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22150
117
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 72619
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22150
117
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 72619
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once again thanks to all .

Thanks,
Pallavi
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try
 
Rob Spoor
Sheriff
Posts: 22150
117
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12981
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Why is the word "abbreviation" so long? And this ad is so short?
ScroogeXHTML - a fast and small RTF to HTML5 and XHTML converter library
https://coderanch.com/t/742149/ScroogeXHTML-RTF-HTML-XHTML-converter
reply
    Bookmark Topic Watch Topic
  • New Topic