Repository navigation
libgis: Add a seed option parsing function - #8033
Merged
Merged
Conversation
The random seed is the option seed in the eleven other tools which take one, all through G_OPT_M_SEED, and only r.sim.water and r.sim.sediment call it random_seed. The key in the history and the run summary is seed already. The old name keeps working through the renamed options list, with the parser's warning, as the earlier renames of these tools do. Written with the help of Claude Code.
Tools which take a seed option each parse and check the seed on their own, with different rules and messages, since the parser itself checks an integer option only loosely: 12abc and 1.5 pass as integers. G_random_seed_from_option() reads the answer with strtoll(), refuses what is not wholly an integer or is outside the range from -2^31 to 2^32 - 1 with a message naming the option, and returns the seed as int64_t after checking the range on the long long, so the narrowing is exact. It does nothing beyond parsing and checking; recording the seed or generating one stays with the tool. r.sim.water and r.sim.sediment use it in place of their own parsing, which also drops the throwaway state they built to check the range before reading the inputs. Their tests match the range message by words, since it is wrapped. Written with the help of Claude Code.
# Conflicts: # raster/r.sim/r.sim.sediment/tests/r_sim_sediment_random_test.py # raster/r.sim/r.sim.water/tests/r_sim_water_random_test.py
wenzeslaus
marked this pull request as ready for review
October 8, 2026 02:24
petrasovaa
reviewed
Oct 8, 2026
…range G_random_parse_seed() reads a decimal integer from a text and says, when it is not a seed, what a seed must be; the seed option function and the environment variables both call it, with their own messages. A value from GRASS_RANDOM_SEED or SOURCE_DATE_EPOCH outside -2^31 to 2^32 - 1 is now an error, as from the option, instead of being reduced to its low 32 bits with a warning: a seed cut to its low bits is what the checks exist to prevent, and a value beyond long long no longer gets a message of its own. Written with the help of Claude Code.
petrasovaa
approved these changes
Oct 16, 2026
This was referenced Oct 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Tools which take a seed option each parse and check the seed on their own, with different rules and messages, since the parser checks an integer option only loosely. This adds one libgis function which reads and checks the seed option with the same rules everywhere, and makes r.sim.water and r.sim.sediment use it. The parsing itself is a new function,
G_random_parse_seed(), which the environment variables use too, so a value ofGRASS_RANDOM_SEEDoutside the range is now an error instead of being reduced to its low 32 bits with a warning. What a tool does without the option, such as generating a seed, stays with the tool.Follows the r.sim migration (#7980) and the rename of its seed option (#8032), both merged.
Details (AI generated)
The function
G_random_seed_from_option(const struct Option *seed)in the newlib/gis/random_options.c, called afterG_parser()with an option which has an answer. The answer is read withstrtoll(), and a value which is not an integer, has trailing characters, or is outside -2147483648 to 4294967295 is a fatal error naming the option, for exampleInvalid random seed <12abc> for seed=: not an integer. The range is checked on thelong longbefore it is narrowed to theint64_tthe layout functions take. The function neither prints nor records the seed; the tool does that.An earlier version also took the flag to generate a seed and required exactly one of the two. No tool wants that rule: r.mapcalc seeds automatically when the expression calls rand() and is phasing out
-s, r.sim generates a seed without the option, and tools such as r.surf.random and v.random have no flag, so the function reads the option only.The parser
const char *G_random_parse_seed(const char *text, int64_t *seed)in lrand48.c reads a decimal integer with nothing after it and checks the range; it returns NULL and sets the seed, or a phrase saying what a seed must be, which the caller puts after a colon in a message naming the text and its source. The option function and the environment path are the two callers, each with its own message. The dependency runs from random_options.c to the generator code only; the generator knows nothing of options. A value fromGRASS_RANDOM_SEEDorSOURCE_DATE_EPOCHoutside -2147483648 to 4294967295 was reduced to its low 32 bits with a warning, which was the original behavior; it is now an error like any other bad seed, since a seed cut to its low bits is what the checks exist to prevent. The variables page says so.r.sim
r.sim.water and r.sim.sediment previously parsed
seedwith their ownstrtoll()call and built a throwaway generator state to have the range checked before the inputs are read; they now call the function. The generated seed and the deprecated-sare unchanged. Tests check the range limits are accepted and1e9is refused.Use in other tools
r.mapcalc parses and checks the seed itself today (its migration to the layouts is a separate PR). With the function, the seed handling there becomes:
The code and this description were written with Claude Code (Claude Fable 5.1) and reviewed by the author of the PR.