Skip to content

std.random draws the type its use demands: next() and range(lo, hi) over a Random trait (#2279, D162) - #2747

Merged
ehartford merged 1 commit into
fix-2326bfrom
fix-2279
Oct 26, 2026
Merged

ehartford merged 1 commit into
fix-2326bfrom
fix-2279

Conversation

@ehartford

@ehartford ehartford commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

std.random fixed its results at i32 (next_i32, range_i32, plus
range_isize): range_i32(97, 123) as u8 at a byte consumer, and an i64
interval (range(0, 1 << 40)) could not be drawn at all.

Now next[T: Random]() -> T and range[T: Random](lo: T, hi: T) -> T
take the type from the use (a typed binding or parameter) or the bounds;
a generic result binds from its demand (Law 2), so no new compiler
mechanism is needed. Random is implemented for every integer type up to
64 bits and for f32 and f64, each through one 64-bit helper (signed_in /
unsigned_in over the two's-complement span, unit() for floats), so no
arithmetic trait is required; a user type becomes drawable by
implementing it. seed takes a u64 (D114). The i32 helpers are deleted
(Law 9): List.shuffle and the one-liner documents call range, and the
documents drop their as u8.

Test: behav_2279_random_draws_the_demanded_type (an i64 bound drawing
past the i32 range, a u8 parameter, i8 and u16 bindings, f64 and f32
ranges, empty ranges, a repeated seed). behav_list_stable_sort and
test/complexity/stdlib.w (shuffle) pass on stage1; the documents'
one-liners run.

Delegated under D98 as D162 (brief on #2279, 80%).

Closes #2279

…ver a Random trait (#2279, D162)

std.random fixed its results at i32 (`next_i32`, `range_i32`, plus
`range_isize`): `range_i32(97, 123) as u8` at a byte consumer, and an i64
interval (`range(0, 1 << 40)`) could not be drawn at all.

Now `next[T: Random]() -> T` and `range[T: Random](lo: T, hi: T) -> T`
take the type from the use (a typed binding or parameter) or the bounds;
a generic result binds from its demand (Law 2), so no new compiler
mechanism is needed. `Random` is implemented for every integer type up to
64 bits and for f32 and f64, each through one 64-bit helper (signed_in /
unsigned_in over the two's-complement span, unit() for floats), so no
arithmetic trait is required; a user type becomes drawable by
implementing it. `seed` takes a u64 (D114). The i32 helpers are deleted
(Law 9): List.shuffle and the one-liner documents call `range`, and the
documents drop their `as u8`.

Test: behav_2279_random_draws_the_demanded_type (an i64 bound drawing
past the i32 range, a u8 parameter, i8 and u16 bindings, f64 and f32
ranges, empty ranges, a repeated seed). behav_list_stable_sort and
test/complexity/stdlib.w (shuffle) pass on stage1; the documents'
one-liners run.

Delegated under D98 as D162 (brief on #2279, 80%).
@ehartford
ehartford added this pull request to stack #2749 October 26, 2026 07:19
@ehartford ehartford changed the title fix 2279 std.random draws the type its use demands: next() and range(lo, hi) over a Random trait (#2279, D162) Oct 26, 2026
@ehartford
ehartford marked this pull request as ready for review October 26, 2026 08:24
@ehartford
ehartford merged commit 763bdd8 into main Oct 26, 2026
@ehartford
ehartford deleted the fix-2279 branch October 26, 2026 08:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

std.random should generate the requested type instead of fixing results at i32

1 participant