Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active February 3, 2026 20:18
Show Gist options
  • Select an option

  • Save dacr/317f02ba962be481e38134875947f225 to your computer and use it in GitHub Desktop.

Select an option

Save dacr/317f02ba962be481e38134875947f225 to your computer and use it in GitHub Desktop.
using random.org random generator to generate true random number / published by https://github.com/dacr/code-examples-manager #a79f4f26-80a8-413a-a363-8dbaf3e12267/31a9d62e39194064ee09120269c204e968c91886
// summary : using random.org random generator to generate true random number
// keywords : scala, random, rng
// publish : gist
// authors : David Crosson
// license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt)
// id : a79f4f26-80a8-413a-a363-8dbaf3e12267
// created-on : 2021-06-23T14:09:23+02:00
// managed-by : https://github.com/dacr/code-examples-manager
// 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.3"
// ---------------------
import ujson.Obj, scala.util.Properties.envOrNone
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) =>
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" -> 1,
"min" -> 0,
"max" -> 999,
"n" -> 5,
"replacement" -> false // for distinct values
)
)
val response = requests.post(api, data = query)
println(response.text())
//println(upickle.default.read[Response](response.text()))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment