Returns a random int. The syntax is:
random(max [, seed])
If optional seed is given then the random number generator is first seeded to that value. For the openssl randprovider (the default, here), the seed value is cast to varbyte and the result added to existing entropy; for the system randprovider, the value is cast to long and the C generator is seeded with the result. The random number generator will only be seeded once in each session, and will be randomly seeded on the first call if no seed is supplied. The seed parameter is ignored in the second and later calls to random in a process.
After optional seeding, a random int is generated with the current provider. The returned number is always non-negative, and if max is greater than zero, also less than max. In versions prior to 8.01.1773857975 20260318, the returned number was limited to the C random() limit (e.g. effective max limit of 32768 on some platforms), max was checked if nonzero, and the only randprovider was system.
This function is typically used to either generate a random number for later use, or to generate a random ordering of result records by adding random to the ORDER BY clause.
Examples of typical use:
SELECT NAME, random(100)
FROM SYSTABLES
The results might be:
NAME random(100) SYSTABLES 90 SYSCOLUMNS 16 SYSINDEX 94 SYSUSERS 96 SYSPERMS 1 SYSTRIG 84 SYSMETAINDEX 96 |
SELECT ENAME
FROM EMPLOYEE
ORDER BY random(0);
The results would be a list of employees in a random order.