# Feature Request: Random.generateTask

**URL:** <https://discourse.elm-lang.org/t/feature-request-random-generatetask/4719>\
**Category:** Request Feedback\
**Created:** [November 24, 2019, 6:19am UTC](https://discourse.elm-lang.org/t/feature-request-random-generatetask/4719 "2019-11-24T06:19:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lucas\_Payr](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/lucas_payr/32/4993_2.png) [@Lucas\_Payr](https://discourse.elm-lang.org/u/Lucas_Payr)\
**Post date:** [November 24, 2019, 6:19am UTC](https://discourse.elm-lang.org/t/feature-request-random-generatetask/4719/1 "2019-11-24T06:19:54Z")

</div>

I noticed that Elm/Random has no way to generate a random value as a Task. I’m not sure if this is intended.

Currently, I am trying to build a chat with [Jsonstore](https://www.jsonstore.io/) at the backend.  
If I now want to create a new chatroom, I need to first generate a number and then create the room.

```elm
createRoom : Task Error (Maybe Room)
createRoom =
    Random.generateTask Random.int 0 Random.maxInt
    |> Task.andThen insertNewRoom

```

Instead, I need to split it up into two messages: one for the Random value and one for everything else.

```auto
update : Msg -> Model -> (Model,Cmd Msg)
update msg model =
    case msg of
        PressedNewRoomButton ->
            ( model
            , Random.generate Random.int 0 Random.maxInt
            )

        GeneratedRoomId int ->
            ( model
            , insertNewRoom int
                |> Task.attempt Sync
            )

```

Alternatively I could store a seed in the model, but as this is the only time I use a Random value, I would like to avoid it.

---

<div class="post-metadata">

**Author:** ![MartinS](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/martins/32/3137_2.png) [@MartinS](https://discourse.elm-lang.org/u/MartinS)\
**Post date:** [November 24, 2019, 8:24am UTC](https://discourse.elm-lang.org/t/feature-request-random-generatetask/4719/2 "2019-11-24T08:24:23Z")

</div>

Could you instead create a hash based on the current time or some data in your model that changes often? Then use that hash with Random.initialSeed.

---

<div class="post-metadata">

**Author:** ![choonkeat](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/choonkeat/32/1555_2.png) [@choonkeat](https://discourse.elm-lang.org/u/choonkeat)\
**Post date:** [November 25, 2019, 12:38pm UTC](https://discourse.elm-lang.org/t/feature-request-random-generatetask/4719/3 "2019-11-25T12:38:48Z")

</div>

you could use a `Monad` 🙊 and pretend the split didn’t happen 🙈

```elm
type Msg
    = Monad (Cmd Msg)
    -- ...

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        Monad cmd ->
            ( model, cmd )

        PressedNewRoomButton ->
            ( model
            , Random.generate
                (insertNewRoom
                    >> Task.attempt Sync
                    >> Monad
                )
                (Random.int 0 Random.maxInt)
            )

        Sync result ->
            -- ...

```

---

<div class="post-metadata">

**Author:** ![Lucas\_Payr](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/lucas_payr/32/4993_2.png) [@Lucas\_Payr](https://discourse.elm-lang.org/u/Lucas_Payr)\
**Post date:** [November 26, 2019, 6:18am UTC](https://discourse.elm-lang.org/t/feature-request-random-generatetask/4719/4 "2019-11-26T06:18:33Z")

</div>

I like this approach but I feel like its more of a workaround then a real solution. So i’ve created a new issue over at [elm/random](https://github.com/elm/random/issues/13), let’s see if gets added.

Alternatively, as @MartinS said, I might switch to another RNG package like [mgold/elm-random-pcg](https://package.elm-lang.org/packages/mgold/elm-random-pcg/latest). As it uses `Time.now` for generating numbers.

**Edit:** Just noticed that @mgold’s package is outdated. If mgold doesn’t mind, I might start my own fork and update it to 0.19

---

<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:** [December 6, 2019, 6:22am UTC](https://discourse.elm-lang.org/t/feature-request-random-generatetask/4719/5 "2019-12-06T06:22:59Z")

</div>

Closed on December 6, 2019, 6:22am UTC

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