Created
September 15, 2008 20:53
-
-
Save matthewmccullough/10938 to your computer and use it in GitHub Desktop.
Generate a GUID composed of username, machine name, and random int
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.InetAddress; | |
import java.net.UnknownHostException; | |
import java.util.Map; | |
import java.util.Random; | |
public class GetMachineName { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
for (int i = 0; i < 10; i++) { | |
String uid = getMachineAndUserIdentifer(); | |
System.out.println("UID:" + uid); | |
} | |
} | |
private static String getMachineAndUserIdentifer() { | |
String machineName = null; | |
String userName = null; | |
Integer microGUID = null; | |
//Username | |
Map<String,String> env = System.getenv(); | |
userName = env.get("USER"); | |
//Machine name | |
try { | |
machineName = InetAddress.getLocalHost().getHostName(); | |
} catch (UnknownHostException e) { | |
throw new IllegalStateException("Hostname cannot be found, but is required.", e); | |
} | |
//GUID random number | |
microGUID = new Random().nextInt(); | |
microGUID = Math.abs(microGUID); | |
return userName + "@" + machineName + "@GUID" + microGUID; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment