Skip to main content

rand

1. RAND Function

The RAND function in H2 database is used to generate a random value between 0 (inclusive) and 1 (exclusive).

2. Syntax

The syntax for the RAND function in H2 database is as follows:

RAND()

Arguments

  • This function does not require any arguments.

Return

  • The RAND function returns a random value between 0 (inclusive) and 1 (exclusive).

3. Notes

  • The RAND function in H2 database generates a new random value each time it is called.
  • The generated random value is of type DOUBLE.
  • If you need to generate a random integer within a specific range, you can use the RANDOM function along with FLOOR or CEILING to round the result accordingly.

4. Examples

Here are a few examples demonstrating the usage of the RAND function in H2 database:

Example 1 - Generating a random value:

SELECT RAND() AS random_value;

Output:

random_value
----------------
0.4563116325216

Example 2 - Generating multiple random values:

SELECT RAND() AS random_value1,
RAND() AS random_value2,
RAND() AS random_value3;

Output:

random_value1  | random_value2  | random_value3
---------------|---------------|---------------
0.825367195883 | 0.170303372837 | 0.712188694428
  • RANDOM - Generate a random number within a specified range.
  • floor - Round a number down to the nearest integer.
  • CEILING - Round a number up to the nearest integer.