Last active
May 25, 2024 10:19
-
-
Save dacr/514291bb8808f438ff68a6f33d70a361 to your computer and use it in GitHub Desktop.
generate a random loto play using random.org random generator / published by https://github.com/dacr/code-examples-manager #e6341c73-bbfc-4c53-b340-41b88bb0ec3c/88f858879a04e30bf2c170b0169d8f77e295aa09
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 loto play using random.org random generator | |
// keywords : scala, loto, 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 : e6341c73-bbfc-4c53-b340-41b88bb0ec3c | |
// created-on : 2021-06-23T14:09:23+02:00 | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.4.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, 49).sorted.mkString(",")) | |
println(generate(key, 1, 1, 10).sorted.mkString(",")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment