1

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);
    }
peterh
  • 11,875
  • 18
  • 85
  • 108

1 Answers1

1

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();
itsme86
  • 19,266
  • 4
  • 41
  • 57