• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generating Random Characters from ASCII values  RSS feed

 
Jacob Pager
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For part of my program, I am trying to ask the user for input on generating a number of random characters. I have already done this with integers and doubles, how would I do this for characters using ASCII values? Would I use the same format as I did for generating integers (Shown in code)?


**Update**





 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd use the nextDouble() so I could get the range and starting point, like this:

 
Jacob Pager
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is System.CurrentTimeMillis();?

Also, the output is still not in characters, but in numbers.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Edit: If you want asciirange2 to be inclusive the increment it by one prior to the for() loop.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacob Pager wrote:What is System.CurrentTimeMillis();?


It's a random number seed, see javadoc for java.util.Random

Also, the output is still not in characters, but in numbers.


Huh? the function returns a String.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacob Pager wrote:For part of my program, I am trying to ask the user for input on generating a number of random characters. I have already done this with integers and doubles, how would I do this for characters using ASCII values? Would I use the same format as I did for generating integers?

To be honest, the format (ie, what you want to see) is entirely up to you. The logic, on the other hand, is something different - and I'd say you could do with breaking yours up a bit more.

Right now, the code that displays your random "whatevers" is tied directly to the code that generates them; and that's generally not a good idea. What something IS and what it looks like are two completely different things, and you should try to keep them as separate as possible - which, incidentally, is one of the reasons that every object in Java has a toString() method.

For example, what if you had ato generate ONE random integer within a range? Now your 'random' method might look like:and furthermore, you could use that same randomtInt() method to generate your "characters", because now the business of generating the number is separated from the one of printing them out.

Also: I would strongly advise you NOT to overload methods unless you have no other choice - and if you do, be consistent about it.
Right now, you have two methods called random(), and a third one, which basically does exactly the same thing, with a different name. Why?

ALL your names - fields, constants, classes, parameters and methods - should have names that describe what they do or why they are there, so don't go overloading methods just to save yourself a bit of thought or a few keystrokes.

HIH

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Edit: If you want asciirange2 to be inclusive the increment it by one prior to the for() loop.

@Jacob: And the same goes for my example.

Almost all "range" methods in Java - especially ones involving integers - are of the form:
  public whatever doSomethingWithARange(int inclusiveLow, int exclusiveHigh) { ...
and it's generally a good pattern to stick to.

The reasons are too numerous to go into in this thread, so you'll just have to trust me on this one for now.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!