Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Random seed

 
gradnash nash
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All, I hope this is the right forum for posting this thread.

I want to know how I can use the Random class, and get the time.

I know i can do like this right

java.utilRandom rand = new Random(System.getCurrentTimeInMilliseconds());


This would give me the current time right?

I would like to know how I can convert this to a string.

Help would be greatly appreciated.

In essense I would like to use Random, seed it with the current time and convert it to a String. Is this possible , or is there an alternative.

Thanks
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused about what you're trying to do.

An instance of Random (see API) will provide you with a "stream of pseudorandom numbers." If desired, you can seed Random with a long, so you could use System.currentTimeMillis() -- although I don't see why you would use this in place of Random's no-args constructor.

With an instance of Random, you can get "random" primitives by calling the appropriate methods. You can then convert these to Strings using wrapper class toString methods. For example...

Can you describe more about the output you're trying to get?
 
gradnash nash
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.

I didnt phrase the question correctly sorry, here i will try and explain what i would like to do.

I would like to append date to a long number.

So I thought one can use the java.util.Random class, get a long random number, then somehow append date to it.

This i need to convert to a string and display.

Hope I have explained what needs to be done properly this time.

A reply would be appreciated, some examples too would be good.

thanks
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code snippet I posted above should help you get a random primitive and convert that to a String.

System.currentTimeMillis() will provide a long representation of the current time, which you can convert to a String simply by concatenating with the previous String (using the + operator).
 
gradnash nash
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.

I will try what you have suggested and get back for more help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic