Packages

Client for ANU QNRG service (random number from quantum fluctuations of the vacuum)

Current section

Files

Jump to
qrandom lib qrandom.ex
Raw

lib/qrandom.ex

defmodule Qrandom do
@moduledoc """
Client for QRNG service of Australian National University.
"""
@api_url "https://qrng.anu.edu.au/API/jsonI.php?length=1&type=uint16"
@max_uint16 65535
@doc """
Random integer number between 0-65535.
"""
def int() do
case HTTPoison.get(@api_url) do
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
case Poison.decode(body) do
{:ok, response} ->
List.first(response["data"])
_ ->
{:error, "It's not possible to parse response from ANU QRNG API"}
end
_ ->
{:error, "It's not possible to get a random number from ANU QRNG API"}
end
end
@doc """
Random float number between 0.0-1.0.
"""
def float() do
case int() do
qint when is_integer(qint) -> qint / @max_uint16
error -> error
end
end
end