Last active
March 29, 2025 08:39
-
-
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/e3dde0a9401d2b15aab1aebb4f10eb238921e4a4
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
// 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.6.4" | |
//> using dep "com.lihaoyi::requests:0.9.0" | |
//> using dep "com.lihaoyi::upickle:4.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