Skip to content

Instantly share code, notes, and snippets.

@matthewmccullough
Created September 15, 2008 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save matthewmccullough/10938 to your computer and use it in GitHub Desktop.
Save matthewmccullough/10938 to your computer and use it in GitHub Desktop.
Generate a GUID composed of username, machine name, and random int
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