Java tutorial
//package com.java2s; public class Main { public static String getSessionId() { StringBuilder s = new StringBuilder(); for (int i = 8; i-- > 0;) { s.append(getSessionComponent()); switch (i) { case 1: case 3: case 5: case 7: s.append("-"); break; } } return s.toString(); } private static String getSessionComponent() { return Integer.toHexString((int) ((Math.random() + 1) * 0x10000)).substring(1); } }