RAND Function in BigQuery

The RAND function in BigQuery is used to generate a pseudo-random FLOAT64 value between 0 (inclusive) and 1 (exclusive). It is widely used for generating random IDs.

Syntax

SQL

RAND()

The function takes no arguments and returns a different value for each row in the result set.

Example 1: Generating a basic random number.

SQL

-- Returns a value like 0.4582103
SELECT RAND() AS random_value;

Example 2: Generating a random integer between 1 and 100.

SQL

-- Uses FLOOR and arithmetic to scale the random value
SELECT CAST(FLOOR(RAND() * 100) + 1 AS INT64) AS random_int;