BigQuery · Math function
BigQuery RAND
RAND() returns a pseudo-random FLOAT64 from 0 (inclusive) to 1 (exclusive), different for every row and every run. Use it for random sampling with WHERE RAND() < 0.1 and for shuffling. It takes no seed; for a repeatable sample, hash a stable key with FARM_FINGERPRINT.
RAND generates a new pseudo-random number each time it is evaluated: 0 is possible, 1 is not. Scale it for other ranges, for example CAST(FLOOR(RAND() * 6) AS INT64) + 1 for an integer from 1 to 6. RAND takes no arguments, so RAND(42) fails with No matching signature for function RAND; BigQuery has no seeded random numbers.
WHERE RAND() < 0.01 keeps about 1% of rows, but BigQuery still reads the full columns, so on-demand bytes billed are the same as for the whole table. TABLESAMPLE SYSTEM (1 PERCENT) (Preview) reads a random subset of storage blocks and is billed only for those; the docs suggest combining the two for a cheaper row-level sample.
Because the values change on every run, RAND is a poor fit for A/B assignment, train and test splits or anything you need to reproduce. Hash a stable ID instead: ABS(MOD(FARM_FINGERPRINT(user_id), 100)) < 10 selects the same 10% of users every time, in every table. A non-recursive CTE that calls RAND runs once per reference, so two references to it see different numbers.
Syntax
RAND()Return value
FLOAT64 in the range [0, 1): 0 inclusive, 1 exclusive. A new value for every row and every evaluation.
Examples
Random integer from 1 to 6
FLOOR(RAND() * n) gives an integer from 0 to n - 1. The value changes on every run, so there is no fixed result.
SELECT CAST(FLOOR(RAND() * 6) AS INT64) + 1 AS dice_roll;Row-level vs block-level sampling
The first query bills the full columns of the table. The second reads about 1% of the table's storage blocks. BigQuery splits tables into blocks only above about 1 GB, so a smaller table can be a single block and is then read in full.
-- Row-level: reads the full columns, keeps about 1% of rows
SELECT user_id, event_name
FROM `project.dataset.events`
WHERE RAND() < 0.01;
-- Block-level: reads about 1% of the storage blocks
SELECT user_id, event_name
FROM `project.dataset.events` TABLESAMPLE SYSTEM (1 PERCENT);A repeatable sample with FARM_FINGERPRINT
Each user_id always hashes to the same bucket, so this query keeps the same 107 of 1,000 users on every run, unlike WHERE RAND() < 0.1.
WITH users AS (
SELECT CONCAT('user_', CAST(n AS STRING)) AS user_id
FROM UNNEST(GENERATE_ARRAY(1, 1000)) AS n
)
SELECT COUNT(*) AS sampled_users
FROM users
WHERE ABS(MOD(FARM_FINGERPRINT(user_id), 100)) < 10;| sampled_users |
|---|
| 107 |
Shuffle rows or pick a few at random
ORDER BY RAND() puts rows in random order, and LIMIT keeps a handful. Every row that passes the WHERE clause is still read, so filter on the partitioning column first.
SELECT order_id, customer_id
FROM `project.dataset.orders`
WHERE order_date >= '2026-11-01'
ORDER BY RAND()
LIMIT 20;Common mistakes
- Passing a seed.
RAND(42)fails; there is no seeded RAND. Use FARM_FINGERPRINT on a stable key for repeatable pseudo-random values. - Expecting
WHERE RAND() < 0.1to cut cost. It returns fewer rows but bills the same bytes; TABLESAMPLE SYSTEM reads fewer blocks. - Splitting train and test sets with RAND. Each run gives a different split; use
ABS(MOD(FARM_FINGERPRINT(id), 10)) < 8for a stable 80/20 split. - Reading RAND values from a CTE twice. Non-recursive CTEs run once per reference, so the two reads disagree; write the values to a table first.
- Calling RAND() twice in one SELECT and expecting the same number. Each call returns a new value; compute it once in a subquery and reference the column.
Cost and performance
WHERE RAND() < xdoesn't reduce bytes scanned: on-demand cost is the same as reading the full columns.- TABLESAMPLE SYSTEM reads and bills only the sampled blocks. Because it samples whole blocks, samples from partitioned or clustered tables tend to be more biased than samples from tables that are neither.
- Queries that call RAND are non-deterministic, so BigQuery doesn't serve them from the query cache and every run is billed again.
FAQ
How do I select a random sample of rows in BigQuery?
Use WHERE RAND() < 0.01 for about 1% of rows, or TABLESAMPLE SYSTEM (1 PERCENT) to also read less data. For a sample that stays the same across runs, filter on ABS(MOD(FARM_FINGERPRINT(id), 100)) < 1.
Can I set a seed for RAND in BigQuery?
No. RAND takes no arguments. Use FARM_FINGERPRINT on a stable column, optionally concatenated with a salt string, to get deterministic pseudo-random values.
How do I generate a random number between two values?
Use min_value + RAND() * (max_value - min_value) for a FLOAT64, or CAST(FLOOR(RAND() * (max_value - min_value + 1)) AS INT64) + min_value for an integer from min_value to max_value inclusive.
Related
Errors you may hit
More mathematical functions
All BigQuery mathematical functions · All functions · BigQuery error messages
