Created
June 26, 2021 13:52
-
-
Save supermario/a7efafdb675e1af295b5a456c78d9f6a to your computer and use it in GitHub Desktop.
RandomOrg.elm
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
module RandomOrg exposing (..) | |
import Http | |
import Json.Decode as D | |
import Prng.Uuid as Uuid | |
import Process | |
import Random.Pcg.Extended as Random | |
import Task exposing (Task) | |
{- Usage: | |
type Msg | |
= RandomSeedResult (Result Http.Error String) | |
| ... | |
cmd = RandomOrg.get5Random32Bit RandomSeedResult | |
update msg model = | |
RandomSeedResult result -> | |
( { model | |
| uuid = | |
RandomOrg.toMaybeUuid result | |
} | |
, Cmd.none | |
) | |
... | |
Idea courtesy of @jxxcarlson on elm-lang.slack.com | |
-} | |
toMaybeUuid result = | |
let | |
newUuid = | |
case parsed of | |
seed :: seedExtension -> | |
Random.initialSeed seed seedExtension | |
|> Random.step Uuid.generator | |
|> Tuple.first | |
|> Uuid.toString | |
|> Just | |
_ -> | |
Nothing | |
parsed = | |
case result of | |
Ok s -> | |
s | |
|> String.split "\n" | |
|> List.map String.toInt | |
|> justs | |
Err e -> | |
[] | |
in | |
newUuid | |
urlGet5Random32Bit = | |
"https://www.random.org/integers/?num=5&min=-1000000000&max=1000000000&col=1&base=10&format=plain&rnd=new" | |
get5Random32Bit msg = | |
Http.get { expect = Http.expectString msg, url = urlGet5Random32Bit } | |
justs = | |
List.foldl | |
(\v acc -> | |
case v of | |
Just el -> | |
[ el ] ++ acc | |
Nothing -> | |
acc | |
) | |
[] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment