|
发表于 2018-7-22 06:58:40
|
显示全部楼层
广东省东莞市
我改造了一下,加入到自己的类库中去使用。
因为我认为这个只做为一个静态函数使用要更好些。如果什么都是类,那类就太多了,个人愚见。
/// <summary>
/// 获取随机IP
/// </summary>
/// <returns></returns>
public static String GetRandomIP()
{
var f = new Func<int, string>((int ip) =>
{
int[] b = new int[4];
String x = "";
b[0] = (int)((ip >> 24) & 0xff);
b[1] = (int)((ip >> 16) & 0xff);
b[2] = (int)((ip >> 8) & 0xff);
b[3] = (int)(ip & 0xff);
x = Convert.ToString(b[0]) + "." + Convert.ToString(b[1]) + "." + Convert.ToString(b[2]) + "." + Convert.ToString(b[3]);
return x;
});
var rdint = otherHelp.RndSeed();
//ip范围
int[][] range = {
new int[]{607649792,608174079},//36.56.0.0-36.63.255.255
new int[]{1038614528,1039007743}, //61.232.0.0-61.237.255.255
new int[]{1783627776,1784676351}, //106.80.0.0-106.95.255.255
new int[]{2035023872,2035154943}, //121.76.0.0-121.77.255.255
new int[]{2078801920,2079064063}, //123.232.0.0-123.235.255.255
new int[]{-1950089216,-1948778497}, //139.196.0.0-139.215.255.255
new int[]{-1425539072,-1425014785}, //171.8.0.0-171.15.255.255
new int[]{-1236271104,-1235419137}, //182.80.0.0-182.92.255.255
new int[]{-770113536,-768606209}, //210.25.0.0-210.47.255.255
new int[]{-569376768,-564133889}, //222.16.0.0-222.95.255.255
};
var index = rdint.Next(10);
var res=f(range[index][0] + new Random().Next(range[index][1] - range[index][0]));
return res;
} |
|