How do I randomly equalize unequal values?

前端 未结 1 1179
忘掉有多难
忘掉有多难 2021-01-24 08:52

Say I have multiple unequal values a, b, c, d, e. Is it possible to turn these unequal values into equal values just by using random number generation?

Example: a=100, b

1条回答
  •  伪装坚强ぢ
    2021-01-24 09:21

    Well, there is a technique called Inverse Weights, where you sample items inverse proportional to their previous appearance. Each time we sample a, b, c, d or e, we update their appearance numbers and recalculate probabilities. Simple python code, I sample numbers [0...4] as a, b, c, d, e and start with what you listed as appearances. After 100,000 samples they looks to be equidistributed

    import numpy as np
    
    n = np.array([100, 140, 200, 2, 1000])
    
    for k in range(1, 100000):
    
        p  = (1.0 / n) # make probabilities inverse to weights
        p /= np.sum(p) # normalization
    
        a = np.random.choice(5, p = p) # sampling numbers in the range [0...5)
    
        n[a] += 1 # update weights
    
    print(n)
    

    Output

    [20260 20194 20290 20305 20392]
    

    0 讨论(0)
提交回复
热议问题