/ support / ebsSupport / test_spread.mjs
test_spread.mjs
 1  const MULTI_001 = "001"
 2  const MULTI_004 = "004"
 3  const MULTI_016 = "016"
 4  const MULTI_024 = "024"
 5  const MULTI_072 = "072"
 6  const MULTI_108 = "108"
 7  const MULTI_240 = "240"
 8  
 9  const ALL_MULTI = [
10    MULTI_001, MULTI_004, MULTI_016,
11    MULTI_024, MULTI_072, MULTI_108,
12    MULTI_240
13  ]
14  
15  const MULTI_PRICES = new Map([
16    [MULTI_004,   25],
17    [MULTI_016,   84],
18    [MULTI_024,  192],
19    [MULTI_072,  696],
20    [MULTI_108, 1097],
21    [MULTI_240, 2662]
22  ])
23  
24  const MULTI_CHANCES = new Map(ALL_MULTI.slice(1).map( (m) => [m, 1-(1/(+m/2))] ))
25  
26  function chanceSelect(options) {
27    const chance = Math.random()
28    ,     sorted = [...options.entries()].sort(([vA,pA], [vB,pB]) => pA > pB)
29  
30    let min = 0
31  
32    for (const [t, p] of sorted) {
33      if (chance > min && chance <= p+min) {
34        return t
35      }
36      min += p
37    }
38  }
39  
40  function frequency(arr) {
41    const init = Array.from(new Set(arr)).map((k) => [k, 0])
42    const freq = new Map(init)
43  
44    for (const el of arr) {
45      freq.set(el, freq.get(el)+1)
46    }
47  
48    return freq
49  }
50  
51  function main() {
52    const targetRevenue = 2000000 
53    let total = 0
54    let purchases = new Array()
55  
56    while (total < targetRevenue) {
57      const selected = chanceSelect(MULTI_CHANCES)
58      total += MULTI_PRICES.get(selected)
59      purchases.push(selected)
60    }
61  
62    console.log(frequency(purchases))
63    console.log(total)
64  }
65  
66  main()