Skip to content

r.surf.random: Draw from an exact random number layout - #8089

Merged
wenzeslaus merged 5 commits into
OSGeo:mainfrom
wenzeslaus:r-surf-random-layout
Oct 22, 2026
Merged

wenzeslaus merged 5 commits into
OSGeo:mainfrom
wenzeslaus:r-surf-random-layout

Conversation

@wenzeslaus

Copy link
Copy Markdown
Member

r.surf.random draws its values from a caller-owned random number state instead of the shared generator, with each row as one unit of an exact random number layout. The maps for a given seed are identical to earlier versions, and the seed is read with the standard function for the seed option.

This brings little new besides the unified seed handling, a warning for regions with more cells than one seed holds, and the use of the new API as an example. However, the cost is small, a few lines to manage the state. It is a first step towards making r.surf.random parallel.

Details (AI generated)

The change

  • Each row is a unit of an exact layout of one value per cell, placed with G_random_state_for_unit() before the row is filled. The float map draws with G_random_double(). The integer map draws with G_random_uint32(), which returns what (unsigned int)G_mrand48() gave. Both maps are therefore byte-identical to earlier versions for a given seed.
  • A region with more cells than one seed holds gets a warning, as the rows then repeat earlier rows plus a constant.
  • The seed is read with G_random_seed_from_option(), so a seed which is not wholly an integer or is outside -2147483648 to 4294967295 is an error naming the option, before the output exists. It was truncated with strtol() before. Without the option a seed is generated with G_random_generate_seed(), as before.

Tests

A test checks that a seed beyond each end of the range and a seed with trailing text are refused and that no map is created. The pinned-value tests of the tool already on main pass with the change.

Verification

Float and integer maps for 8 seeds across the whole range were identical to those of the binary before this change.

Suggested squash commit message

r.surf.random: Draw from an exact random number layout
r.surf.random now draws from a random number state of its own instead
of the generator shared by the whole program. Each row is a unit of an
exact random number layout with one value per cell, so the maps for a
given seed stay the same as in earlier versions. The integer map uses
G_random_uint32(), which returns what (unsigned int)G_mrand48() gave.
This is a first step towards filling the rows in parallel.

A warning is given when the region has more cells than one seed holds.
The seed is read with G_random_seed_from_option(), so a seed out of
range or with trailing text is an error instead of being truncated.

Written with the help of Claude Code.

The code and this description were written with Claude Code (Claude Fable 5.1 and Claude Opus 5.5) and reviewed by the author of the PR.

Each row is a unit of an exact layout drawing one value per cell, so
every row starts where it started in a serial run and the maps stay
identical for a given seed, while the row loop no longer depends on the
shared generator. The integer map takes G_random_uint32(), the bits
G_mrand48() gave. The seed is read with G_random_seed_from_option(),
which refuses what is not wholly an integer or is outside the range the
generator uses, instead of the seed being truncated or silently reduced
to its low 32 bits.
A seed out of range or with text after the integer is an error, and
no map is created. The test covers the values beyond both ends of the
range G_random_seed_from_option() accepts and a value with trailing text.

Written with the help of Claude Code.
The rows are still filled one after another, so a comment about
getting the values of a serial run in any order does not apply yet.

Written with the help of Claude Code.
The warning joined two complete clauses with a semicolon and had no
final period. The style guide asks for periods after complete
sentences, and most messages with two sentences use them.

Written with the help of Claude Code.
@github-actions github-actions Bot added raster Related to raster data processing Python Related code is in Python C Related code is in C module tests Related to Test Suite labels Oct 20, 2026
@wenzeslaus
wenzeslaus force-pushed the r-surf-random-layout branch from 97f0fe7 to e5905e2 Compare October 20, 2026 04:10
Comment thread raster/r.surf.random/tests/r_surf_random_test.py Outdated
ToolError is what grass.tools raises. Its parent CalledModuleError is
kept for code moving from run_command-style calls, and new code should
not rely on it.

Written with the help of Claude Code.
@wenzeslaus
wenzeslaus enabled auto-merge (squash) October 22, 2026 02:32
@wenzeslaus
wenzeslaus merged commit 3c7208d into OSGeo:main Oct 22, 2026
26 checks passed
@wenzeslaus
wenzeslaus deleted the r-surf-random-layout branch October 22, 2026 02:33
@github-actions github-actions Bot added this to the 8.6.0 milestone Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C Related code is in C module Python Related code is in Python raster Related to raster data processing tests Related to Test Suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants