I'm on a project where I have to implement an A/B split in 15 or so views, in this case for PHP - we'd like to use the same math if possible for our JavaScript projects.
What is the most ideal, least verbose, least CPU-intensive way of doing this? For this project, I just need to set a variable: something like:
// In the main controller
if(rand(1, 2) == 2)
{
$recipe = 'program';
}
else
{
$recipe = 'standard';
}
define('RECIPE',$recipe);
// In the view
$program = (RECIPE == 'program') ? '&ProgramOfInterest=' . $program_id : '';
We have 20 or so devs here and we all have our ways - what is the best, benchmark-proven way?
You should use mt_rand()
over rand()
. It's 4x faster than rand()
because mt_rand
uses a Mersenne Twister over the libc random number generator which rand()
uses (see php.net).
You can then get an equivalent to mt_rand()
for javascript from the php.js library.
least cpu-intensive way:
A team got something like 200Gb/sec of random data like this :)
Then simply:
var counter = 0;
if(imageBit[counter++]){
:D
I assume that the A/B split needs to be consistent across all users, so a user should consistently fall in the A or the B bucket (if not, your analysis of the A/B buckets will not reveal any info related to page navigation).
Hence using a rand function is probably not what you want.
Instead use a session identifier, session cookie or persistent cookie, and simply use the last 3 bytes of that cookie instead of your random value. You can add the bytes or multiply their ascii values to generate a number which you can the use as your cut-off.
This would be very portable across PHP and JS, and it is cheap in CPU and easy to verify correctness in a unit test.