# Generating a Random Number in BigQuery

If you're looking to generate a random number in BigQuery, check out the RAND() function.

It's a pseudo-random number generator, generating a float in the interval \[0, 1).

I've used it a few times before, but for the today's exercise, I've decide to try something akin to Python's random.pick(). So, let's pick a random value from an ARRAY.

Inspired by one of [Mikhail Berlyant's SO answers](https://stackoverflow.com/questions/56780571/generate-a-random-value-from-an-array-in-google-bigquery-standard-sql) (which are some of the best answers on BigQuery on SO, linked in comments), I wanted to randomly assign one of 20 options to 100 participants.

As seen in [one of my previous posts](https://datawise.dev/accessing-array-elements-in-bigquery) about accessing array elements, we're going to generate a random 0-based index to retrieve the random array element in each case.

`OFFSET(CAST(ARRAY_LENGTH(available_options)*RAND()-0.5 AS INT64))`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1715852267451/66fe198e-64d9-4e3f-a5d2-190418205aca.jpeg align="center")

*Found it useful? Subscribe to my Analytics newsletter at*[*notjustsql.com*](http://notjustsql.com)*.*
