# Generating random numbers without Cmd

**URL:** <https://discourse.elm-lang.org/t/generating-random-numbers-without-cmd/6108>\
**Category:** Show and Tell\
**Created:** [July 30, 2020, 8:05pm UTC](https://discourse.elm-lang.org/t/generating-random-numbers-without-cmd/6108 "2020-07-30T20:05:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![DullBananas](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/dullbananas/32/3709_2.png) [@DullBananas](https://discourse.elm-lang.org/u/DullBananas)\
**Post date:** [July 30, 2020, 8:05pm UTC](https://discourse.elm-lang.org/t/generating-random-numbers-without-cmd/6108/1 "2020-07-30T20:05:59Z")

</div>

I was bored so I made a hacky way to generate random numbers without using Cmd:

[https://ellie-app.com/9yQqJKWS2Tta1](https://ellie-app.com/9yQqJKWS2Tta1)

It works by defining this Elm function:

```elm
generate : D.Value -> Float
generate =
    D.decodeValue (D.field "n" D.float) >> Result.withDefault 0

```

and this object which is sent via flags and stored in the model:

```javascript
{
    get n() {
        return Math.random();
    }
}

```

Don’t do this in production code; it’s extremely hacky

---

<div class="post-metadata">

**Author:** ![allanderek](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/allanderek/32/1360_2.png) [@allanderek](https://discourse.elm-lang.org/u/allanderek)\
**Post date:** [July 30, 2020, 8:57pm UTC](https://discourse.elm-lang.org/t/generating-random-numbers-without-cmd/6108/2 "2020-07-30T20:57:52Z")

</div>

That’s a clever solution. However it will break the referential transparency of Elm (ie. the promise you get from the lack of side effects that all expressions will, given the same inputs, evaluate to the same output). You’ve broken that which is why your expression `generate model.obj` returns different results even though you’re giving it the same input.

However you don’t need that to avoid `Cmd`, you can just pass a ‘seed’ in as a flag and then use `Random.step`. The issue with that is that `Random.step` will return the random value you have generated _and_ the new `seed`, so you have to remember to update the ‘seed’ that is somehow stored in your model.

---

<div class="post-metadata">

**Author:** ![hans-helmut](https://avatars.discourse-cdn.com/v4/letter/h/d9b06d/32.png) [@hans-helmut](https://discourse.elm-lang.org/u/hans-helmut)\
**Post date:** [August 2, 2020, 5:18pm UTC](https://discourse.elm-lang.org/t/generating-random-numbers-without-cmd/6108/3 "2020-08-02T17:18:49Z")

</div>

> [@allanderek](#):
>
> However it will break the referential transparency of Elm

I expected to break this program after compiling it with `--optimize` and minifiying it using uglify.js and the usual options, but it still works. It seems, according to the documentation of uglify.js, that calls to pure functions are removed, when the result is not used. I miss an optimization, which replaces the call to pure functions with the result, at least for simple types. This seems not to be supported by uglify.js. Are there any options I miss, or other tools, which support this?

---

<div class="post-metadata">

**Author:** ![opvasger](https://avatars.discourse-cdn.com/v4/letter/o/8e8cbc/32.png) [@opvasger](https://discourse.elm-lang.org/u/opvasger)\
**Post date:** [August 3, 2020, 12:38am UTC](https://discourse.elm-lang.org/t/generating-random-numbers-without-cmd/6108/4 "2020-08-03T00:38:59Z")

</div>

Breaking referential transparency seems pretty problematic - I can imagine some ugly uses of this non-feature.

---

<div class="post-metadata">

**Author:** ![allanderek](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/allanderek/32/1360_2.png) [@allanderek](https://discourse.elm-lang.org/u/allanderek)\
**Post date:** [August 3, 2020, 9:59am UTC](https://discourse.elm-lang.org/t/generating-random-numbers-without-cmd/6108/5 "2020-08-03T09:59:52Z")

</div>

I have no idea how uglify determines whether a function is pure or not. In any case, the result _is_ used. The result of `generate` is used to set the `num` field on the model.

However, it wouldn’t surprise me if at some point, some optimisation would indeed break this scheme, just not in this simple case. For example, if you called it twice to generate _two_ random numbers the optimiser in the Elm compiler might recognise the two calls as a common sub-expression.

---

<div class="post-metadata">

**Author:** ![Janiczek](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/janiczek/32/4516_2.png) [@Janiczek](https://discourse.elm-lang.org/u/Janiczek)\
**Post date:** [August 3, 2020, 10:39am UTC](https://discourse.elm-lang.org/t/generating-random-numbers-without-cmd/6108/6 "2020-08-03T10:39:07Z")

</div>

> [@hans-helmut](#):
>
> I miss an optimization, which replaces the call to pure functions with the result, at least for simple types.

[Prepack](https://prepack.io/) sounds like what you want!

---

<div class="post-metadata">

**Author:** ![hans-helmut](https://avatars.discourse-cdn.com/v4/letter/h/d9b06d/32.png) [@hans-helmut](https://discourse.elm-lang.org/u/hans-helmut)\
**Post date:** [August 3, 2020, 6:34pm UTC](https://discourse.elm-lang.org/t/generating-random-numbers-without-cmd/6108/7 "2020-08-03T18:34:55Z")

</div>

> [@allanderek](#):
>
> I have no idea how uglify determines whether a function is pure or not.

It’s given in the command-line by `--compress 'pure_funcs="F2,F3,F4,F5,F6,F7,F8,F9,A2,A3,A4,A5,A6,A7,A8,A9",pure_getters,`…, there is no automatic detection.

---

<div class="post-metadata">

**Author:** ![hans-helmut](https://avatars.discourse-cdn.com/v4/letter/h/d9b06d/32.png) [@hans-helmut](https://discourse.elm-lang.org/u/hans-helmut)\
**Post date:** [August 4, 2020, 7:56pm UTC](https://discourse.elm-lang.org/t/generating-random-numbers-without-cmd/6108/8 "2020-08-04T19:56:54Z")

</div>

> [@Janiczek](#):
>
> [Prepack](https://prepack.io/) sounds like what you want!

Thanks for the suggestion. Unfortunately Prepack is unable to parse the Javascript generated by Elm and the development is stopped at the moment.

---

<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:** [August 14, 2020, 7:56pm UTC](https://discourse.elm-lang.org/t/generating-random-numbers-without-cmd/6108/9 "2020-08-14T19:56:57Z")

</div>

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