Scala随机生成ip代码
def main(args: Array[String]): Unit = {
for (i <- 1 to 100){
println(resetIp())
}
}
def num2ip(ip:Int): String ={
val b = new Array[Int](4)
var x = ""
b(0) = (ip>>24) & 0xff
b(1) = (ip>>16) & 0xff
b(2) = (ip>>8) & 0xff
b(3) = ip & 0xff
x = Integer.toString(b(0)) + "." + Integer.toString(b(1)) + "." + Integer.toString(b(2)) + "." + Integer.toString(b(3))
x
}
def resetIp(): String ={
val range = Array(Array(607649792, 608174079),
Array(1038614528, 1039007743),
Array(1783627776, 1784676351),
Array(2035023872, 2035154943),
Array(2078801920, 2079064063),
Array(-1950089216, -1948778497),
Array(-1425539072, -1425014785),
Array(-1236271104, -1235419137),
Array(-770113536, -768606209),
Array(-569376768, -564133889)
)
val rdint = new Random()
var index = rdint.nextInt(10)
var ip = num2ip(range(index)(0) + rdint.nextInt(range(index)(1) - range(index)(0)))
ip
}
|