Created
June 9, 2022 14:41
-
-
Save Amerousful/293f7033e727eec2693e86128a252de6 to your computer and use it in GitHub Desktop.
Scala, generate random IPv6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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