Last active
May 27, 2023 06:28
-
-
Save dacr/fb5536dfb439a8cfa6e872d212c8c963 to your computer and use it in GitHub Desktop.
generate a random euromillions play using random.org random generator / published by https://github.com/dacr/code-examples-manager #b79ea6ae-c833-422a-8835-90536e79eda7/b532e0d2ff2b2ac6494f027d9eed899f0b8f152a
This file contains 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
// summary : generate a random euromillions play using random.org random generator | |
// keywords : scala, euromillions, random, rng, random-org, fdj, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : b79ea6ae-c833-422a-8835-90536e79eda7 | |
// created-on : 2021-06-23T14:09:23+02:00 | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.2.2" | |
//> using dep "com.lihaoyi::requests:0.8.0" | |
//> using dep "com.lihaoyi::upickle:3.1.0" | |
// --------------------- | |
import ujson.Obj, scala.util.Properties.envOrNone | |
import upickle.default.ReadWriter | |
case class Random(data: List[Int]) derives ReadWriter | |
case class Result(random: Random) derives ReadWriter | |
case class Response(jsonrpc: String, result: Result) derives ReadWriter | |
def generate(key: String, count: Int, first: Int, last: Int): List[Int] = { | |
val api = "https://api.random.org/json-rpc/4/invoke" | |
val query = Obj( | |
"jsonrpc" -> "2.0", | |
"method" -> "generateIntegers", | |
"id" -> 42, | |
"params" -> Obj( | |
"apiKey" -> key, | |
"n" -> count, | |
"min" -> first, | |
"max" -> last, | |
"n" -> count, | |
"replacement" -> false // for distinct values | |
) | |
) | |
val response = requests.post(api, data = query) | |
// println(response.text()) | |
upickle.default.read[Response](response.text()).result.random.data | |
} | |
envOrNone("RANDOM_ORG_API_KEY") match { | |
case None => println("Signup to random.org, create an api key or reuse an existing one - https://api.random.org/dashboard") | |
case Some(key) => | |
println(generate(key, 5, 1, 50).sorted.mkString(",")) | |
println(generate(key, 2, 1, 12).sorted.mkString(",")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment