jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Random seed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Random seed" Watch "Random seed" New topic
Author

Random seed

gradnash nash
Greenhorn

Joined: Mar 14, 2006
Posts: 11
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

Joined: Aug 31, 2004
Posts: 11343

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?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
gradnash nash
Greenhorn

Joined: Mar 14, 2006
Posts: 11
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Mar 14, 2006
Posts: 11
Thanks for the reply.

I will try what you have suggested and get back for more help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Random seed
 
Similar Threads
Unique Random Number?
Date/Time class...?
how to generate key from string
threads + unique random numbers
algorithm for random number