RANDBETWEEN Function

Generates a random integer between a low and a high number. Two inputs may be Integer or Decimal types, functions returning these types, or column references.
• Range is inclusive of the two parameter values.
• The first parameter must be the lower value in the range.

Wrangle vs. SQL: This function is part of Wrangle, a proprietary data transformation language. Wrangle is not SQL. For more information, see Wrangle Language.

Basic Usage

`randbetween(1,10)`

Output: Generates a random Integer value between 1 and `10`.

Syntax and Arguments

`RANDBETWEEN(value1,value2)`

ArgumentRequired?Data TypeDescription
value1YInteger or DecimalInteger or Decimal literal, function returning one of these data types, or a column reference for the lower boundary of the range. Range is inclusive of this value.
value2YInteger or DecimalInteger or Decimal literal, function returning one of these data types, or a column reference for the upper boundary of the range. Range is inclusive of this value.

value1, value2

Literals, functions, or column references to Integer or Decimal values that are used as the lower and upper bounds, respectively, for the range.

• Missing input values generate missing results.
• Multiple columns and wildcards are not supported.

Usage Notes:

Required? Data Type Example Value
YesInteger or Decimal literal, function, or column reference`100`

Examples

Tip: For additional examples, see How-to Guides.

Example - RANDBETWEEN, PI, and TRUNC functions

This example illustrates how you can apply the following functions to generate new and random data in your dataset:

• `RANDBETWEEN` - Generate a random Integer value between two specified Integers. See RANDBETWEEN Function.
• `PI` - Generate the value of pi to 15 decimal points. See PI Function.
• `ROUND` - Round a decimal value to the nearest Integer or to a specified number of digits. See ROUND Function.
• `TRUNC` - Round a value down to the nearest Integer value. See TRUNC Function.

Source:

In the following example, a company produces 10 circular parts, the size of which is measured in each product's radius in inches.

p0011
p0022
p0033
p0044
p0055
p0066
p0077
p0088
p0099
p01010

Based on the above data, the company wants to generate some additional sizing information for these circular parts, including the generation of two points along each part's circumference where quality stress tests can be applied.

Transformation:

To begin, you can use the following steps to generate the area and circumference for each product, rounded to three decimal points:

Transformation Name `New formula` `Single row formula` `ROUND(PI() * (POW(radius_in, 2)), 3)` `'area_sqin'`

Transformation Name `New formula` `Single row formula` `ROUND(PI() * (2 * radius_in), 3)` `'circumference_in'`

For quality purposes, the company needs two tests points along the circumference, which are generated by calculating two separate random locations along the circumference. Since the `RANDBETWEEN` function only calculates using Integer values, you must first truncate the values from `circumference_in`:

Transformation Name `New formula` `Single row formula` `TRUNC(circumference_in)` `'trunc_circumference_in'`

Then, you can calculate the random points using the following:

Transformation Name `New formula` `Single row formula` `RANDBETWEEN(0, trunc_circumference_in)` `'testPt01_in'`

Transformation Name `New formula` `Single row formula` `RANDBETWEEN(0, trunc_circumference_in)` `'testPt02_in'`

Results:

After the `trunc_circumference_in` column is dropped, the data should look similar to the following: