I would like to create a random string (to output to the console for debugging purposes) based off of the current timestamp.
For example, the console would output:
Setting up browser [123456]...
Getting configuration [758493]...
Completed: [758493].
Completed: [123456].
Here the 123456 and 758493 are the random strings I'm trying to generate.
Here's the pseudocode for how I think it can work:
private String random(int len){
long ts = getCurrentTimestamp;
String value = createRandom(len, ts);
//len is the length of the randomString
//and ts is the salt
return value;
}
Can anyone help with the details of this (what needs to be imported), and/or possibly suggest improvements to this?
解决方案
Well it depends on what you mean by "current timestamp". You could use System.currentTimeMillis(), but that won't necessarily be unique - if you call it several times in a short period, you may well get the same results several times. There's also System.nanoTime().
As an alternative, you could use UUID.randomUUID(), either using all the bits or some subset. (If you decide to use a subset, you should choose them carefully. Not all bits in a UUID are equal.)