From b8bd4c17d6244fa3db0639b4b59172f1f8a6428f Mon Sep 17 00:00:00 2001 From: prrao87 Date: Wed, 3 Sep 2025 10:37:10 -0400 Subject: [PATCH 1/2] Add docs for random function --- .../cypher/expressions/numeric-functions.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/content/docs/cypher/expressions/numeric-functions.md b/src/content/docs/cypher/expressions/numeric-functions.md index abe44379..3c014487 100644 --- a/src/content/docs/cypher/expressions/numeric-functions.md +++ b/src/content/docs/cypher/expressions/numeric-functions.md @@ -39,5 +39,35 @@ implemented so far are listed below. | `sign(x)` | returns the sign of x | `sign(-451)` | `-1` | | `sqrt(x)` | returns the square root of x | `sqrt(4.25)` | `2.061553` | | `tan(x)` | returns the tangent of x | `tan(75)` | `-0.420701` | +| `random(x)` | returns a random number in the range | `random()` | `0.910543` | +| `setseed(x)` | sets the seed for the `random()` function above | `setseed(0.2)` | null | + +## Random number generation + +The `random()` function returns a floating point number `x` in the range `0.0 <= x <= 1.0`. +The following example uses the `random()` function to randomize the order of the results +returned by a query: + +```cypher +// Randomize the order in which results are returned +MATCH (p1:Person)-[:KNOWS]->(p2:Person) +RETURN p1.id, p2.id, random() AS r +ORDER BY r LIMIT 3 +``` + +If you want to fix the random seed for the random number generator, you can do so by running the +`setseed(y)` query beforehand. + +```cypher +// Query 1: Set the seed +RETURN setseed(0.2); + +// Query 2: Run the random number generator that uses the specified seed +MATCH (p1:Person)-[:KNOWS]->(p2:Person) +RETURN p1.id, p2.id, random() AS r +ORDER BY r LIMIT 3 +``` + +Setting the seed will ensure that the results are always returned in the same order. From dbf91636cdf44cc48a3ef8070efd4956944bf19d Mon Sep 17 00:00:00 2001 From: prrao87 Date: Wed, 3 Sep 2025 10:43:54 -0400 Subject: [PATCH 2/2] Add note --- src/content/docs/cypher/expressions/numeric-functions.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/content/docs/cypher/expressions/numeric-functions.md b/src/content/docs/cypher/expressions/numeric-functions.md index 3c014487..1086063e 100644 --- a/src/content/docs/cypher/expressions/numeric-functions.md +++ b/src/content/docs/cypher/expressions/numeric-functions.md @@ -71,3 +71,8 @@ ORDER BY r LIMIT 3 ``` Setting the seed will ensure that the results are always returned in the same order. + +:::caution[Note] +The `setseed(y)` function applies globally, meaning that other functions that depend +on random number generation, like `gen_rand_uuid()`, are also affected by setting the seed value. +::: \ No newline at end of file