Skip to content

grass.script.raster: auto-seed rand() when no seed provided#7269

Open
tmchow wants to merge 2 commits intoOSGeo:mainfrom
tmchow:fix/5974-mapcalc-auto-seed
Open

grass.script.raster: auto-seed rand() when no seed provided#7269
tmchow wants to merge 2 commits intoOSGeo:mainfrom
tmchow:fix/5974-mapcalc-auto-seed

Conversation

@tmchow
Copy link
Copy Markdown

@tmchow tmchow commented Apr 3, 2026

mapcalc("random = rand(0.0,1.0)") fails with "Pseudo-random number generator not seeded" because r.mapcalc requires an explicit seed for its rand() function, but the Python wrapper passes None by default.

The seed="auto" path already generates a seed correctly (line 144-145), but users have to know to pass it explicitly. This detects rand( in the expression string and auto-generates a seed when none is provided, matching the existing auto-seed behavior. Applied to both mapcalc() and mapcalc_start().

The check is a simple substring match on the expression before template substitution. It won't false-positive on variable names containing "rand(" since those would be inside ${} template syntax and rand( with a parenthesis is unambiguous as the r.mapcalc function call.

This contribution was developed with AI assistance (Claude Code).

Fixes #5974

mapcalc() and mapcalc_start() fail with 'Pseudo-random number
generator not seeded' when the expression uses rand() and no
explicit seed parameter is passed. Detect rand() in the expression
and auto-generate a seed, matching the existing seed='auto' behavior.

Fixes OSGeo#5974
@github-actions github-actions bot added Python Related code is in Python libraries labels Apr 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libraries Python Related code is in Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] grass.script.mapcalc: Pseudo-random number generator not seeded for raster calculator

2 participants