Syntax
NORMAL(<mean> , <stddev> , <gen>)
Purpose
The NORMAL() function returns a floating-point number that follows the normal distribution (also known as the Gaussian distribution).
meanspecifies the mean, andstddevspecifies the standard deviation.meanandstddevmust be scalar values that do not change with row iteration. For example, they can be integer or float constants, or scalar functions.meanandstddevcan be1,2,ABS(-10), or expressions like@v1and1+@v3in PL.meanandstddevcannot beRAND(),RANDOM(),RAND(2),t1.c1,ABS(-t1.c1), or1+c2.
- The random value generation algorithm is based on
std::normal_distributionand uses the "Marsaglia Polar Method," which consumes a significant amount of CPU resources. genis a value generation function, typically using theRANDOM()function. If the input value is a constant, theNORMAL()function returns a fixed value.
Examples
The following example generates 1 million random values that follow the normal distribution, rounds them to one decimal place, and counts the occurrences.
obclient> SELECT v, COUNT(*) FROM (SELECT ROUND(NORMAL(3, 0.1, RANDOM()), 1) v FROM TABLE(GENERATOR(1000000))) x GROUP BY v ORDER BY v;
+------+----------+
| v | COUNT(*) |
+------+----------+
| 2.5 | 4 |
| 2.6 | 238 |
| 2.7 | 5979 |
| 2.8 | 60889 |
| 2.9 | 241991 |
| 3.0 | 382951 |
| 3.1 | 241306 |
| 3.2 | 60533 |
| 3.3 | 5861 |
| 3.4 | 243 |
| 3.5 | 5 |
+------+----------+
11 rows in set
The following example generates 1 million random values that follow the normal distribution, which takes 1.5 seconds.
obclient> SELECT COUNT(NORMAL(0, 1, RANDOM())) v FROM TABLE(GENERATOR(1000000));
+---------+
| V |
+---------+
| 1000000 |
+---------+
1 row in set (1.49 sec)
