Need help with getting / understanding Random

I love coding in Elm, but I dread having to deal with the Random or elm-community Random packages.

I’m trying to get it to work, and I just don’t understand why it doesn’t. I’m trying at the very basic super-simple level.

I’ve opened up a repl and I’m going through the example in the generate function.

$ elm repl
---- Elm 0.19.1 ----------------------------------------------------------------
Say :help for help and :exit to exit! More at <https://elm-lang.org/0.19.1/repl>
--------------------------------------------------------------------------------
> import Random
> 
> point =
|     Random.pair (Random.int -100 100) (Random.int -100 100)
|   
Generator <function> : Random.Generator ( Int, Int )
> type Msg = NewPoint (Int, Int)
> 
> newPoint =
|     Random.generate NewPoint point
|   
<internals> : Cmd Msg
> newPoint
<internals> : Cmd Msg

The document says:

Each time you run the newPoint command, it will produce a new 2D point like (57, 18) or (-82, 6).

So why do I see <internals> : Cmd Msg instead of a new 2D point? What am I doing wrong? Am I missing something? Do I need to do something further?

Thanks.
– Mitchell

1 Like

In Elm, you don’t directly “run” commands; instead you create Cmds and you “hand them” to the Elm runtime, which runs it for you.
In the REPL, when you input a function/value, the REPL tells you the type of the thing you’ve written. When you create a Cmd, it gives you the type of Cmd, which is actually an “internal” type, as in “closed to view”. Regardless, the type is confirmed to be Cmd Msg, which means that if you use it within an update function, it will work as intended.
As a sidenote, this has nothing to do specifically with Random, it’d be the same with any type of Cmd.

1 Like

This helped. It helped me get over the conceptual block I had. I’ve got Random working now. Thank you @Augustin82

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.