Commit b3c8e86f authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Add REAL std.random(REAL low, REAL high) with associated testcase.

Example usage:

    sub vcl_fetch {
	/* Spread TTLs -10%..+5% because CMS timestamps whole minutes */
	set beresp.ttl = beresp.ttl * random(0.90, 1.05);
    }

This usage was one of the very first VCL examples we ever wrote on
a whiteboard, we just never got a round tuit until now.

Fixes: #793



git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@5421 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent e42e4234
# $Id$
test "Test std.random"
# needs random generator
random
server s1 {
rxreq
txresp -proto HTTP/1.0 -nolen -bodylen 9
} -start
varnish v1 -vcl+backend {
import std from "${topbuild}/lib/libvmod_std/.libs/libvmod_std.so.1" ;
sub vcl_fetch {
set beresp.http.rnd1 = std.random(0,1);
set beresp.http.rnd2 = std.random(0,10);
set beresp.http.rnd3 = std.random(8,10);
set beresp.http.rnd4 = std.random(99,100);
}
} -start
# NB: Do not change the number 1
# NB: Only srandom(1) is standardized as deterministic.
varnish v1 -cliok "debug.srandom 1"
client c1 {
txreq
rxresp
expect resp.http.rnd1 == 0.420
expect resp.http.rnd2 == 1.972
expect resp.http.rnd3 == 8.783
expect resp.http.rnd4 == 99.399
expect resp.bodylen == 9
} -run
......@@ -30,3 +30,4 @@ Init init_function
Function STRING toupper(PRIV_CALL, STRING_LIST)
Function STRING tolower(PRIV_VCL, STRING_LIST)
Function VOID set_ip_tos(INT)
Function REAL random(REAL, REAL)
......@@ -120,3 +120,16 @@ init_function(struct vmod_priv *priv, const struct VCL_conf *cfg)
priv->free = free;
return (0);
}
double
vmod_random(struct sess *sp, double lo, double hi)
{
double a;
(void)sp;
a = random() / (double)(1ULL<<32);
a *= hi - lo;
a += lo;
return (a);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment