Available in all subroutines.

Returns the true numerator or denominator of the time called. For example, randombool(3,4) will return true ¾ of the time (about 75%). This is useful for situations like A/B testing where you want to serve one web page to 90% of the requests and another to 10% of the requests. This example is shown below.

Formally, randombool picks a pseudorandom value (0 ≤ r ≤ RAND_MAX). It then returns true if (RAND_MAX × numerator) > (r × denominator). This will return true when r/RAND_MAX < numerator/denominator.

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

This function is not prefixed with the std. namespace.


if (randombool(1, 10)) {
set req.http.X-ABTest = "A";
} else {
set req.http.X-ABTest = "B";

Try it out

randombool 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.

Early expiry of cached objects

Cached a large number of objects for too long and want to update and shorten their TTLs.

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.