Skip to content

v.random: Draw from a random number state of its own - #8094

Merged
wenzeslaus merged 4 commits into
OSGeo:mainfrom
wenzeslaus:v-random-state
Oct 22, 2026
Merged

wenzeslaus merged 4 commits into
OSGeo:mainfrom
wenzeslaus:v-random-state

Conversation

@wenzeslaus

Copy link
Copy Markdown
Member

With this PR, v.random draws its points from a caller-owned random number state instead of the generator shared by the whole program. The state is a single sequence seeded with G_random_state_from_seed(), which gives the same values as the shared generator. So every case, including the rejection loops for restricted areas, keeps its points for a given seed.

The seed is now read with the standard function for the seed option. Previously, the text of the seed was converted with atoi(), which ignored any text after the number and did not check the range.

Details (AI generated)

The change

  • One struct G_random_state seeded from the seed replaces the function pointer to G_drand48(). The points and the rejection loops now draw from it.
  • The seed is now read with G_random_seed_from_option(). A seed which is not wholly an integer or is outside -2147483648 to 4294967295 is an error naming the option, reported before the output map is created. Previously, atoi() ignored trailing text and did not check the range. 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

2D and 3D points for 9 seeds across the whole range and points restricted to an area for 2 seeds were identical to those of the binary before this change.

Suggested squash commit message

v.random: Draw from a random number state of its own
v.random now draws its points from one random number state seeded with
G_random_state_from_seed() instead of the generator shared by the whole
program. The state gives the values the shared generator gave, so every
case, including the rejection loops for restricted areas, keeps its
points for a given seed.

The seed is now read with G_random_seed_from_option(), so a seed out of
range or with trailing text is an error, reported before the output map
is created. Previously, atoi() ignored trailing text and did not check
the range.

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.

The points come from a single sequence seeded with
G_random_state_from_seed(), which gives exactly the values the shared
generator gave, so every case, including the rejection loops for
restricted areas, keeps its points for a given seed. 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 to int by atoi(), and it is checked before the output
map is created.
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 comment only says what the placement of the code shows.

Written with the help of Claude Code.
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.
@github-actions github-actions Bot added vector Related to vector data processing Python Related code is in Python C Related code is in C module tests Related to Test Suite labels Oct 21, 2026
@wenzeslaus
wenzeslaus merged commit 47597a9 into OSGeo:main Oct 22, 2026
26 checks passed
@wenzeslaus
wenzeslaus deleted the v-random-state branch October 22, 2026 02:31
@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 tests Related to Test Suite vector Related to vector data processing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants