It is the right approach and you can get it working like this:
var rand = Math.random();
if (rand < 0.5) {
// first variant
} else if (rand < 0.7) {
// second variant
} else {
// third variant
}
You don't need to completly specify the intervals in conditions because Math.random()
generates real numbers from interval [0,1].
For dynamic amount of variants you can create array of corresponding right ends of intervals and loop trough it:
var intervals = [0.1, 0.25, 0.28] // note that the array has to be sorted for this to work
var rand = Math.random();
for(i in intervals) {
if (rand < intervals[i]) {
// i-th variant
break;
}
}