@@ -39,5 +39,40 @@ implemented so far are listed below.
39
39
| ` sign(x) ` | returns the sign of x | ` sign(-451) ` | ` -1 ` |
40
40
| ` sqrt(x) ` | returns the square root of x | ` sqrt(4.25) ` | ` 2.061553 ` |
41
41
| ` tan(x) ` | returns the tangent of x | ` tan(75) ` | ` -0.420701 ` |
42
+ | ` random(x) ` | returns a random number in the range | ` random() ` | ` 0.910543 ` |
43
+ | ` setseed(x) ` | sets the seed for the ` random() ` function above | ` setseed(0.2) ` | null |
42
44
43
45
</div >
46
+
47
+ ## Random number generation
48
+
49
+ The ` random() ` function returns a floating point number ` x ` in the range ` 0.0 <= x <= 1.0 ` .
50
+ The following example uses the ` random() ` function to randomize the order of the results
51
+ returned by a query:
52
+
53
+ ``` cypher
54
+ // Randomize the order in which results are returned
55
+ MATCH (p1:Person)-[:KNOWS]->(p2:Person)
56
+ RETURN p1.id, p2.id, random() AS r
57
+ ORDER BY r LIMIT 3
58
+ ```
59
+
60
+ If you want to fix the random seed for the random number generator, you can do so by running the
61
+ ` setseed(y) ` query beforehand.
62
+
63
+ ``` cypher
64
+ // Query 1: Set the seed
65
+ RETURN setseed(0.2);
66
+
67
+ // Query 2: Run the random number generator that uses the specified seed
68
+ MATCH (p1:Person)-[:KNOWS]->(p2:Person)
69
+ RETURN p1.id, p2.id, random() AS r
70
+ ORDER BY r LIMIT 3
71
+ ```
72
+
73
+ Setting the seed will ensure that the results are always returned in the same order.
74
+
75
+ ::: caution [ Note]
76
+ The ` setseed(y) ` function applies globally, meaning that other functions that depend
77
+ on random number generation, like ` gen_rand_uuid() ` , are also affected by setting the seed value.
78
+ :::
0 commit comments