Stream: ideas

Topic: record builder for randomness


view this post on Zulip Richard Feldman (Jan 09 2025 at 16:01):

I just realized record builders could be used to generate several random values at once, as long as each of them depend only on a seed and don't need each other:

generator : Generator { a : I32, b : I32, c : I32 }
generator = { Random.map2 <-
    a: Random.i32,
    b: Random.i32,
    c: Random.i32,
}

view this post on Zulip Richard Feldman (Jan 09 2025 at 16:01):

all we need to do is to introduce Random.map2

view this post on Zulip Richard Feldman (Jan 09 2025 at 16:01):

no need to thread the seed through

view this post on Zulip Richard Feldman (Jan 09 2025 at 16:03):

this doesn't work if (for example) b needs to depend on a, but the common case we've been talking about is where we need to generate several values independently, which it turns out we can already do if we just add Random.map2 :smiley:

view this post on Zulip Brendan Hansknecht (Jan 09 2025 at 16:08):

Yeah, I feel like this came up before. It maybe was an off hand suggested at one point. I remember seeing a record pattern match to access multiple generated values after a record builder.

view this post on Zulip Brendan Hansknecht (Jan 09 2025 at 16:10):

Cause next line would be :

(seed_, {a, b, c}) = Random.gen seed_, generator

view this post on Zulip Richard Feldman (Jan 09 2025 at 16:11):

yep!

view this post on Zulip Richard Feldman (Jan 09 2025 at 16:11):

although I do wonder how much seed threading we even need to do if we use record builders to batch stuff

view this post on Zulip Richard Feldman (Jan 09 2025 at 16:12):

might still be some, but probably significantly less

view this post on Zulip Brendan Hansknecht (Jan 09 2025 at 16:17):

Really depends how often rng is dependent.

view this post on Zulip Sam Mohr (Jan 09 2025 at 17:59):

Luke and I came to the same conclusion with roc-random: https://github.com/lukewilliamboswell/roc-random/blob/6260860c2e9c8cf52f6cd846583d4a005f4e6b9a/examples/color.roc#L12


Last updated: Jun 16 2026 at 16:19 UTC