2

I'm split testing landing pages in campaign currently by sending them to the following script:

$link[] = "http://www.website.com/lp1";
$link[] = "http://www.website.com/lp2";

$random_number = rand(0,count($link)-1);
$redirectlink = $link[$random_number];

header("Location: $redirectlink");

If I wanted to have the first LP shown 75% of the time, how do I go about doing that? Will simply replicating that first link two more times work, or is there a better way to go about it?

  • "I wanted to have the first LP shown 75% of the time." That's not weighted. That's a guarantee. – webbiedave Mar 29 '12 at 21:48
  • possible duplicate of [Weighted random numbers](http://stackoverflow.com/questions/1761626/weighted-random-numbers). The solution presented is language independent. – webbiedave Mar 29 '12 at 21:48
  • possible duplicate of [Generating random results by weight in PHP?](http://stackoverflow.com/questions/445235/generating-random-results-by-weight-in-php) – Peter O. Oct 20 '12 at 07:08

1 Answers1

2

Maybe there is more better way , but this is working too

$link[0] = array('link' => 'http://example.com/1', 'percent' => 7);
$link[1] = array('link' => 'http://example.com/2', 'percent' => 20);
$link[2] = array('link' => 'http://example.com/3', 'percent' => 73);

$percent_arr = array();
foreach($link as $k => $_l) {
    $percent_arr = array_merge($percent_arr, array_fill(0, $_l['percent'], $k));
}

$random_key = $percent_arr[mt_rand(0,count($percent_arr)-1)];
$redirectlink = $link[$random_key]['link'];

header("Location: $redirectlink");
safarov
  • 7,793
  • 2
  • 36
  • 52