Skip to content

Instantly share code, notes, and snippets.

@Amerousful
Created June 9, 2022 14:41
Show Gist options
  • Select an option

  • Save Amerousful/293f7033e727eec2693e86128a252de6 to your computer and use it in GitHub Desktop.

Select an option

Save Amerousful/293f7033e727eec2693e86128a252de6 to your computer and use it in GitHub Desktop.
Scala, generate random IPv6
import java.util.concurrent.ThreadLocalRandom
val scope = (0 to 9) ++ ('a' to 'f')
def randomElement[T](seq: T*): T = {
seq(ThreadLocalRandom.current.nextInt(seq.size))
}
def generateIp: String = (1 to 32).foldLeft("") { (acc, _) =>
acc + randomElement(scope: _*)
}.grouped(4).mkString(":")
println(generateIp)
// a337:18a6:740c:3d0d:396d:bf5d:8a55:ca6d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment