randomint

INTEGERrandomintINTEGERfromINTEGERto

Available inall subroutines.

Returns a random integer value between from and to, inclusive.

This does not use secure random numbers and should not be used for cryptographic purposes.

Even though integer values in VCL are typically 64-bit signed integers, this VCL function only accepts from, to, and seed, which are signed 32-bit integers. The range of valid values for from, to, and seed is therefore −(231) to 231 − 1 inclusive. That is, the minimum and maximum values are -2147483648 and 2147483647 respectively, or -0x80000000 and 0x7FFFFFFF in hexadecimal.

This function is not prefixed with the std. namespace.

Example

if (randomint(0, 99) < 5) {
set req.http.X-ABTest = "A";
} else {
set req.http.X-ABTest = "B";
}
if (randomint(-1, 0) == -1) {
set req.http.X-ABTest = "A";
} else {
set req.http.X-ABTest = "B";
}

Try it out

randomint is used in the following code examples. Examples apply VCL to real-world use cases and can be deployed as they are, or adapted for your own service. See the full list of code examples for more inspiration.

Click RUN on a sample below to provision a Fastly service, execute the code on Fastly, and see how the function behaves.

Random integers

Generate random whole numbers in a range.

Set Google Analytics _ga cookie

Due to ITP 2.1 restrictions, cookies set in JavaScript may be limited to a 7-day TTL. Set your Google Analytics cookie on the edge to avoid this.