Using math.random for A/B testing

94
March 13, 2018, at 5:32 PM

Currently I have setup a piece of code to use for A/B testing based on 50/50. But now I want to rebuild it to a 50/25/25 setup. What is the best way to define the Math.random numbers?

// Launch A/B test surveys
if (/GB|CA|MY|EG|US|Global/i.test(_satellite.getVar("dlCountry")) && /EN/i.test(_satellite.getVar("dlLanguage"))) {
    if (!_satellite.readCookie("s_UbVar")) {
        // Make 50/50 split and set cookie
        var UbVariant = RegExp(/true/).test(String(Math.random() < 0.5));
        if (UbVariant) {
            _satellite.setCookie("s_UbVar", "varA");
        } else {
            _satellite.setCookie("s_UbVar", "varB");
        }
    } else {
        // Extend session cookie
        _satellite.setCookie("s_UbVar", _satellite.readCookie("s_UbVar"));
        if (_satellite.readCookie("s_UbVar") === "varA") {
            usabilla_live("trigger", "survey-A");
        } else if (_satellite.readCookie("s_UbVar") === "varB") {
            usabilla_live("trigger", "survey-B");
        } else {
            // Cookies contains an empty/unknown value
            _satellite.removeCookie("s_UbVar");
        }
    }
} else {
    // Remove existing cookies
    _satellite.removeCookie("s_UbVar");
}
Answer 1

Just setup your points like such

var variant = Math.random();
if (variant < 0.5) {
    // A Code
}
else if (variant > 0.75) {
    // B Code
}
else {
    // C Code
}
Answer 2

Or without if's:

const tt = Math.random()
switch (true) {
  case (tt<.5):
    // do 1
    break
  case (tt<.75):
    // do 2
    break
  default:
    // do 3
}
READ ALSO
FinalCountdown.js stop event handler not working

FinalCountdown.js stop event handler not working

i am using the adminLTE template and within that I have a FinalCountdownjs timer as follows:

106
.addClass() jQuery how to specify the CSS

.addClass() jQuery how to specify the CSS

I have a div and I want to put a display none when I click on itI am using bootstrap4 and It has a css style called "d-none" that put display none to an element

99
Strange bug. Javascript, , Trying to run a loop, but getting a blank error in Chrome console

Strange bug. Javascript, , Trying to run a loop, but getting a blank error in Chrome console

I'm running a loop on an array received from an obj {message:Array(22)} ajax callYou can see the array has length = 22

82
jquery Nestable is not scrolling while list is long

jquery Nestable is not scrolling while list is long

I'm using Nestablejs for drag/drop I'm displaying that list (on which drag/drop required) in a modal(bootstrap modal), Everything is working fine for me, the issue is when list (within

83