Reputation:
I'm new to coding. I need to know how to generate random IPv6 in C Sharp. I found this code which generated random IPv4, how can i alter it for IPv6?
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);
}
Upvotes: 2
Views: 1533
Reputation: 19526
What are the constraints around the generated address? If none, it's pretty simple. This should work:
byte[] bytes = new byte[16];
new Random().NextBytes(bytes);
IPAddress ipv6Address = new IPAddress(bytes);
string addressString = ipv6Address.ToString();
Upvotes: 2