# Question about Random.generate and its first argument

**URL:** <https://discourse.elm-lang.org/t/question-about-random-generate-and-its-first-argument/2232>\
**Category:** Learn\
**Created:** [October 16, 2018, 7:11pm UTC](https://discourse.elm-lang.org/t/question-about-random-generate-and-its-first-argument/2232 "2018-10-16T19:11:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![matekm](https://avatars.discourse-cdn.com/v4/letter/m/898d66/32.png) [@matekm](https://discourse.elm-lang.org/u/matekm)\
**Post date:** [October 16, 2018, 7:11pm UTC](https://discourse.elm-lang.org/t/question-about-random-generate-and-its-first-argument/2232/1 "2018-10-16T19:11:42Z")

</div>

Hi,

I’ve just done the chapter about generating random numbers from the official elm guide and there is something that I can’t understand.

In the code I defined message type:

> type Msg =  
> RollDice  
> | NewFace Int

and then in the update function when RollDice event is received I call the Random.generate in following way:

> Random.generate NewFace (Random.int 1 6)

and that is the bit that confuses in: in docs for Random packages I can read that Random.generate have following signature:

> random: (a -\> msg) -\> Generator a -\> Cmd msg

How it’s possible that NewFace “fits” into (a -\> msg)?

---

<div class="post-metadata">

**Author:** ![dmy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/dmy/32/702_2.png) [@dmy](https://discourse.elm-lang.org/u/dmy)\
**Post date:** [October 16, 2018, 7:15pm UTC](https://discourse.elm-lang.org/t/question-about-random-generate-and-its-first-argument/2232/2 "2018-10-16T19:15:14Z")

</div>

`NewFace` is actually a function of type `Int -> Msg`, which is a valid argument for `generate` that accepts the more general `a -> msg`, provided that you also pass it a `Generator Int`, and it will then return a `Cmd Msg`.

So the type inferred at compilation for this `generate` use will be:

```auto
generate: (Int -> Msg) -> Generator Int -> Cmd Msg

```

---

<div class="post-metadata">

**Author:** ![brian](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/brian/32/2180_2.png) [@brian](https://discourse.elm-lang.org/u/brian)\
**Post date:** [October 16, 2018, 7:34pm UTC](https://discourse.elm-lang.org/t/question-about-random-generate-and-its-first-argument/2232/3 "2018-10-16T19:34:31Z")

</div>

elaborating on @dmy’s answer, **defining a custom type generates functions to construct values of their type**. Sometimes you’ll see the cases of a custom type being called “constructors”. This is why! So if I had this type:

```elm
type Msg =
  Hello String

```

Then I can both construct it: `Hello "matekm"`

and match on it:

```elm
case msg of
    Hello whom ->
        Html.text ("Hello, " ++ whom)

```

This holds for custom types with multiple arguments. That means that:

```elm
type Demonstration
    = NoArguments -- has the signature `Demonstration`
    | OneArgument String -- has the signature `String -> Demonstration`
    | TwoArguments Int String -- has the signature `Int -> String -> Demonstration`

```

So in your case:

```elm
type Msg
    = RollDice -- has the signature `Msg`
    | NewFace Int -- has the signature `Int -> Msg`

```

And since `Int -> Msg` matches `a -> msg`, the call to `Random` typechecks!

---

<div class="post-metadata">

**Author:** ![matekm](https://avatars.discourse-cdn.com/v4/letter/m/898d66/32.png) [@matekm](https://discourse.elm-lang.org/u/matekm)\
**Post date:** [October 17, 2018, 5:37am UTC](https://discourse.elm-lang.org/t/question-about-random-generate-and-its-first-argument/2232/4 "2018-10-17T05:37:12Z")

</div>

thank you! That explanation really make things clear!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [October 27, 2018, 5:42am UTC](https://discourse.elm-lang.org/t/question-about-random-generate-and-its-first-argument/2232/5 "2018-10-27T05:42:07Z")

</div>

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