randomint

INTEGERrandomintINTEGERfromINTEGERto

Available in all 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.

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 solution recipes, which show real world use cases. Click RUN 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.

User contributed notes

We welcome comments that add use cases, ideas, tips, and caveats. All comments will be moderated before publication. To post support questions, visit our support center and we'll find you the help you need.