Stream: beginners

Topic: Reading N bytes from a file


view this post on Zulip Joseph Montanaro (Oct 05 2025 at 16:04):

I'd like to generate some random numbers, so I looked around and found roc-random. It doesn't help you with seeding the RNG though, so I figured I could just pull a few bytes from /dev/urandom, but I'm not sure how to do that.

File.read_bytes! wants to read until EOF, which wouldn't work with /dev/urandom.

I see File.open_reader!, but the only function that accepts a Reader seems to be File.read_line!. Technically I guess that would work, since /dev/urandom will probably yield a \n byte before too long, but I feel like there's probably a better way?

view this post on Zulip Brendan Hansknecht (Oct 05 2025 at 19:54):

This totally could be a gap in the basic cli api

view this post on Zulip Brendan Hansknecht (Oct 05 2025 at 19:54):

I mean it probably should have a random seed generating function in general

view this post on Zulip Joseph Montanaro (Oct 07 2025 at 01:08):

Yeah a proper "system random" function would be great b/c it could abstract away the cross-platform details.

Is the basic-cli platform going to stay in Rust? I'd be happy to contribute a PR if that seems worthwhile, I'm certainly not a expert but it's basically a matter of pulling in a crate and wiring up a function or two

view this post on Zulip Anton (Oct 07 2025 at 09:03):

Thanks @Joseph Montanaro, a PR would be great, if you provide claude code or codex with the changes from https://github.com/roc-lang/basic-cli/pull/380/files as an example, it will likely be able to take care of the whole thing.

view this post on Zulip Anton (Oct 07 2025 at 09:05):

Is the basic-cli platform going to stay in Rust?

I believe Richard wanted to port it to zig but I don't see a problem with adding random support in Rust for now.

view this post on Zulip Joseph Montanaro (Oct 07 2025 at 10:19):

Thanks! Should it go in an existing module, or get its own? The only module it sort of fits with would be Env, I guess? But it's not a great fit even there. Seems a little weird to have a whole module just for one function, but that's how Sleep is, after all.

view this post on Zulip Luke Boswell (Oct 07 2025 at 10:22):

I think another module is ok. We might find additional functionality we want in future.


Last updated: Oct 18 2025 at 12:13 UTC