Skip to content

Commit

Permalink
fixup! Fix generateExperimentConfigVal
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon-edge committed Oct 23, 2023
1 parent 92a4b71 commit dd582a1
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/experimentConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

/**
Expand All @@ -40,20 +39,27 @@ const experimentDistribution = {
const generateExperimentConfigVal = <T>(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]
}
}

Expand Down

0 comments on commit dd582a1

Please sign in to comment.