# How to conditionally update list items with random generator

**URL:** <https://discourse.elm-lang.org/t/how-to-conditionally-update-list-items-with-random-generator/8565>\
**Category:** Learn\
**Created:** [August 11, 2022, 9:54am UTC](https://discourse.elm-lang.org/t/how-to-conditionally-update-list-items-with-random-generator/8565 "2022-08-11T09:54:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jigargosar](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/jigargosar/32/79_2.png) [@jigargosar](https://discourse.elm-lang.org/u/jigargosar)\
**Post date:** [August 11, 2022, 9:54am UTC](https://discourse.elm-lang.org/t/how-to-conditionally-update-list-items-with-random-generator/8565/1 "2022-08-11T09:54:07Z")

</div>

# Scenario 1

- If an item in a collection was clicked, we would like to randomly change its properties. Color, scale etc.
- Following code shows a **failed** attempt:

```auto
randomUpdate: Item -> Generator Item
-- implementation omitted for brevity.

updateItem: Id -> Seed -> List Item -> (List Item, Seed)
updateItem id seed = 
   List.map (\ item -> 
     if item.id==id then 
       Random.step (randomUpdate item) seed -- ERROR
     else 
       item
   )

```

### Question

- What would be a simple way to implement `updateItem`?

# Scenario 2

- I write quite a bit of code that deals with random numbers. Mostly for animations, games and generative art.
- I start out by hard-coding values. And later, passing model & seed along the update chain
- So functions requiring randomness can use them while updating both the model & seed.

### Question

How to handle these scenarios when iteratively coding a project?

1. Pass seed & model
2. Wrap update methods in generators
3. Or …?

**FYI** : For non-update scenario, I always prefer to write and combine Generators. The problem starts when I have to update existing values with them. Most of the time the update depth is 2 or 3 levels. Model → Collection → Record

Regards,  
Jigar Gosar

---

<div class="post-metadata">

**Author:** ![passiomatic](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/passiomatic/32/1250_2.png) [@passiomatic](https://discourse.elm-lang.org/u/passiomatic)\
**Post date:** [August 11, 2022, 10:14am UTC](https://discourse.elm-lang.org/t/how-to-conditionally-update-list-items-with-random-generator/8565/2 "2022-08-11T10:14:44Z")

</div>

About Scenario 1: if I understand correctly your issue, when you need to “carry over” something while looping you need to do a `List.foldl`, which in the end will produce a new list with updated (or not) items and a seed to save somewhere for the next round.

---

<div class="post-metadata">

**Author:** ![grigorious](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/grigorious/32/3549_2.png) [@grigorious](https://discourse.elm-lang.org/u/grigorious)\
**Post date:** [August 14, 2022, 9:23am UTC](https://discourse.elm-lang.org/t/how-to-conditionally-update-list-items-with-random-generator/8565/3 "2022-08-14T09:23:07Z")

</div>

I would split the process into two steps:  
click → save the cell coordinates we are modifying to the model and call generator  
generator → produces message for cell update

```elm
type Msg
    = GotRandomColor Colors
    | Randomize

type alias Colors =
    { red : Int
    , green : Int
    , blue : Int
    }

colorsGenerator : Random.Generator Colors
colorsGenerator =
    Random.map3 Colors colorGenerator colorGenerator colorGenerator

colorGenerator : Random.Generator Int
colorGenerator =
    Random.int 0 255

generateRandomColors : Cmd Msg
generateRandomColors =
    Random.generate GotRandomColor colorsGenerator

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        Randomize ->
            -- save position here
            ( model, generateRandomColors )

        GotRandomColor color ->
            ( { model | color = color }, Cmd.none )

```

Here is the **[Ellie link](https://ellie-app.com/jkdBFQCwjN3a1)** for a generator example

I made something similar for a snake game (using random to spawn an apple): **[Snake](https://experiments.klemenc.io/snake)**

P.S. an updated **[Ellie with a grid pattern](https://ellie-app.com/jkgXBB749Vga1)** - click on any square to change the color … not the most elegant code - but the point is splitting the process into 2 steps.

---

<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 24, 2022, 9:23am UTC](https://discourse.elm-lang.org/t/how-to-conditionally-update-list-items-with-random-generator/8565/4 "2022-08-24T09:23:36Z")

</div>

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