java在代码中生成随机数字和ID的两个方法
import java.util.UUID;
import java.util.Random;
public class randomID {
public static void main(String[] args) {
// TODO Auto-generated method stub
UUID uuid = UUID.randomUUID();
String randomId = uuid.toString();
System.out.println("随机 ID: " + randomId);
String randomId2 = generateRandomId();
System.out.println("随机 ID: " + randomId2);
}
public static String generateRandomId() {
long timestamp = System.currentTimeMillis();
Random random = new Random();
int randomNumber = random.nextInt(10000);
return timestamp + "_" + randomNumber;
}
}