From dd582a1c6041a5c9667d916a44a0269b4649096b Mon Sep 17 00:00:00 2001 From: Jon Tzeng Date: Mon, 23 Oct 2023 14:55:56 -0700 Subject: [PATCH] fixup! Fix generateExperimentConfigVal --- src/experimentConfig.ts | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/experimentConfig.ts b/src/experimentConfig.ts index 3129b4d9a67..a13c39116ae 100644 --- a/src/experimentConfig.ts +++ b/src/experimentConfig.ts @@ -24,13 +24,12 @@ const DEFAULT_EXPERIMENT_CONFIG: ExperimentConfig = { const experimentConfigDisklet = makeReactNativeDisklet() -// The probability (0-1) of a feature config being set to the first value(s): -// the configuration that differs from the default feature configuration. +// The probability of an experiment config feature being set for a given key const experimentDistribution = { - swipeLastUsp: [0.5], - createAccountType: [0.5], + swipeLastUsp: [50, 50], + createAccountType: [50, 50], legacyLanding: [0], - createAccountText: [0.33, 0.33] + createAccountText: [33.33, 33.33, 33.33] } /** @@ -40,20 +39,27 @@ const experimentDistribution = { const generateExperimentConfigVal = (key: keyof typeof experimentDistribution, configVals: T[]): T => { const variantProbability = experimentDistribution[key] - if (variantProbability.length !== configVals.length - 1) { + if (variantProbability.length !== configVals.length) { console.error(`Misconfigured experimentDistribution for: '${key}'`) } else { + // Distribute the probability of each config value + const totalProbability = variantProbability.reduce((sum, probability) => sum + probability, 0) + if (totalProbability > 101 || totalProbability < 99) { + console.warn(`Config values for ${key} do not add up to 100% +/- 1%`) + } + const distributedProbabilities = variantProbability.map(probability => probability / totalProbability) + // Generate a random number between 0 and 1 const random = Math.random() // Check which index the random number falls into and return the configVal: let lowerBound = 0 - let upperBound = variantProbability[0] - for (let i = 0; i < variantProbability.length; i++) { + let upperBound = distributedProbabilities[0] + for (let i = 0; i < distributedProbabilities.length; i++) { if (random >= lowerBound && random < upperBound) return configVals[i] lowerBound = upperBound - upperBound += variantProbability[i] + upperBound += distributedProbabilities[i] } }