调用getRandomIp()方法即可
Framework3.5 +使用LINQ

public string getRandomIp()
{ /*
int[][]
这个叫交错数组,白话文就是数组的数组.
初始化的方法:
int[][] numbers = new int[][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
当然也可以使用{}初始化器初始化
int[][] numbers = { new int[] {2,3,4},
new int[] {5,6,7,8,9}
};
*/
int[][] range = {new int[]{,},//36.56.0.0-36.63.255.255
new int[]{,},//61.232.0.0-61.237.255.255
new int[]{,},//106.80.0.0-106.95.255.255
new int[]{,},//121.76.0.0-121.77.255.255
new int[]{,},//123.232.0.0-123.235.255.255
new int[]{-,-},//139.196.0.0-139.215.255.255
new int[]{-,-},//171.8.0.0-171.15.255.255
new int[]{-,-},//182.80.0.0-182.92.255.255
new int[]{-,-},//210.25.0.0-210.47.255.255
new int[]{-,-}, //222.16.0.0-222.95.255.255
}; Random rdint = new Random();
int index = rdint.Next();
string ip = num2ip(range[index][] + new Random().Next(range[index][] - range[index][]));
return ip;
} /*
* 将十进制转换成ip地址
*/
public string num2ip(int ip)
{
int[] b = new int[];
string x = "";
//位移然后与255 做高低位转换
b[] = (int)((ip >> ) & 0xff);
b[] = (int)((ip >> ) & 0xff);
b[] = (int)((ip >> ) & 0xff);
b[] = (int)(ip & 0xff);
x = (b[]).ToString() + "." + (b[]).ToString() + "." + (b[]).ToString() + "." + (b[]).ToString(); return x;
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
class Program
{
static Dictionary<int, int> ips = new int[][]
{
Enumerable.Range(, - + ).ToArray(),
Enumerable.Range(, - + ).ToArray(),
Enumerable.Range(, - + ).ToArray(),
Enumerable.Range(, - + ).ToArray(),
Enumerable.Range(, - + ).ToArray(),
Enumerable.Range(-, - + + ).ToArray(),
Enumerable.Range(-, - + + ).ToArray(),
Enumerable.Range(-, - + + ).ToArray(),
Enumerable.Range(-, - + + ).ToArray(),
Enumerable.Range(-, - + + ).ToArray()
}.SelectMany(x => x).Select((x, i) => new { x, i }).ToDictionary(x => x.i, x => x.x); static string IpToString(int IP)
{
return string.Join(".", BitConverter.GetBytes(IP).Reverse().Select(x => ((int)x).ToString()));
} static int GenIP()
{
return ips[new Random(Guid.NewGuid().GetHashCode()).Next(, ips.Count)];
} static void Main(string[] args)
{
for (int i = ; i < ; i++)
Console.WriteLine(IpToString(GenIP()));
}
}
}

加了比较不容易理解部分的注释..想弄明白的同学想必现在已经明白了吧

05-01 03:07