randombool

BOOLrandomboolINTEGERnumeratorINTEGERdenominator

Available inall 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.

Informally, you have a bag with denominator balls. numerator of these balls are orange. The rest are blue. randombool reaches into the bag and, without looking, pulls out a ball. It then returns true if the ball is orange, and false if it is blue.

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

Early expiry of cached objects

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