Generate valid random internal IPv6 subnet for throw-away testing

278 views Asked by At

I want to create a Docker network for testing purposes. I do not care what particular subnet I use, just that it is private and available. Since the IPv6 address space is so large, I believe that I can generate a subnet randomly and have low chance of collisions with existing subnets.

What is a procedure for generating such a subnet in .NET (C# or F# is fine)?

My initial attempt:

open System
open System.Net

let generateRandomSubnet () =
  let bytes = Array.zeroCreate 16
  let random = Random()

  random.NextBytes(bytes)

  let ipAddress = IPAddress(bytes)
  let subnet = (string ipAddress) + "/24"

  subnet

c726:f7e1:ae0b:93f8:e60c:a921:8398:6a45/24

356a:6035:683c:53a6:7f60:2724:3fc0:bcbf/24

8943:dfb0:a19:b251:723b:341e:d107:9948/24

0

There are 0 answers

Related Questions in F#