Project: QGIS

Version: 3.12

Feature: allow to seed random functions

  • rand(10, 80, 1) → 30 Returns a random integer within the range specified by the minimum and maximum argument (inclusive). If a seed is provided, the returned will always be the same, depending on the seed.
  • randf(10, 80, 1) → 19.37136508087729 Returns a random float within the range specified by the minimum and maximum argument (inclusive). If a seed is provided, the returned will always be the same, depending on the seed.

This PR adds an optional seed parameter to rand() and randf() functions, as per issue #20630 This is very useful if you want the result to be deterministic, for instance to assign random but fixed colors to features. Using color_hsb(rand(0,360,$id),50,50) for instance yields always the same color for the same feature. By the way, the PR also improves the rand() function, which didn't work for high values (over 32000) by using Qt's QRandomGenerator instead of qrand (which it seems was deprecated in Qt 5.11).

This feature was developed by olivierdalang