生成指定长度的随机数,获取本机ip,mac

ipadmac 2018-09-18

例一:

public static String randomCreditKey(int strLength) {

Random rm = new Random();

double pross = (1 + rm.nextDouble()) * Math.pow(10, strLength);

String fixLenthString = String.valueOf(pross);

return fixLenthString.substring(2, strLength + 2);

}

String randomNum = IpUtil.randomCreditKey(16)+IpUtil.randomCreditKey(10)+IpUtil.randomCreditKey(10);

 例二:

public static String getRandom(int paramInt)

  {

    String str = "abcdefghijklmnopqrstuvwxyz0123456789";

    Random localRandom = new Random();

    StringBuffer localStringBuffer = new StringBuffer();

    for (int i = 0; i < paramInt; i++)

    {

      int j = localRandom.nextInt(str.length());

      localStringBuffer.append(str.charAt(j));

    }

    return localStringBuffer.toString();

  }

String randomNum =GuardUtil.getRandom(36);

public static String getLocalMac() {

Enumeration var0 = null;

try {

var0 = NetworkInterface.getNetworkInterfaces();

} catch (SocketException var7) {

var7.printStackTrace();

}

while (true) {

if (var0.hasMoreElements()) {

NetworkInterface var1 = (NetworkInterface) var0.nextElement();

byte[] var2 = null;

try {

var2 = var1.getHardwareAddress();

if (var2 == null) {

continue;

}

} catch (SocketException var8) {

var8.printStackTrace();

}

StringBuffer var3 = new StringBuffer("");

for (int var4 = 0; var4 < var2.length; ++var4) {

if (var4 != 0) {

var3.append("-");

}

int var5 = var2[var4] & 255;

String var6 = Integer.toHexString(var5);

if (var6.length() == 1) {

var3.append("0" + var6.toUpperCase());

} else {

var3.append(var6.toUpperCase());

}

}

return var3.toString();

}

return "00-50-56-C0-00-08";

}

}

public static String getLocalIp() {

try {

return InetAddress.getLocalHost().getHostAddress();

} catch (UnknownHostException localUnknownHostException) {

localUnknownHostException.printStackTrace();

}

return "192.168.2.16";

}

相关推荐