我是编码的新手。我需要知道如何在C Sharp中生成随机IPv6。 我发现这个代码生成随机IPv4,我该如何改变它?
static string GenerateIP()
{
// generate an IP in the range [50-220].[10-100].[1-255].[1-255]
return RNG.Next(50, 220).ToString() + "." + RNG.Next(10, 100).ToString() + "." + RNG.Next(1, 255).ToString() + "." + RNG.Next(1, 255).ToString();
}
}
class RNG
{
private static Random _rng = new Random();
public static int Next(int min, int max)
{
return _rng.Next(min, max);
}
答案 0 :(得分:0)
生成的地址有哪些限制?如果没有,那很简单。这应该有效:
byte[] bytes = new byte[16];
new Random().NextBytes(bytes);
IPAddress ipv6Address = new IPAddress(bytes);
string addressString = ipv6Address.ToString();