我想以伪随机的方式创建一个来自半开间隔的事务值数组,例如[1,5), [5,10), [10,25)
等。区间中的每个数字都有相同的机会被选中,但我想调整一些区间超过另一些区间的概率。流程应该是这样的:
1: Pick any of the defined intervals > Pick a random number in the interval > Append number in array
2: Pick any of the defined intervals > Pick a random number in the interval > Append number in array
...
n: Pick any of the defined intervals > Pick a random number in the interval > Append number in array
我的代码是:
import numpy as np
# set interval limits
a,b,c,d,e,f,g,h,i,j = 1,5,10,25,50,100,200,300,500,1000
# start picking random numbers
np.random.seed(33)
trx_value = np.random.choice([np.random.uniform(a,b),
np.random.uniform(b,c),
np.random.uniform(c,d),
np.random.uniform(d,e),
np.random.uniform(e,f),
np.random.uniform(f,g),
np.random.uniform(g,h),
np.random.uniform(h,i),
np.random.uniform(i,j)], 20,
p=[0.1, 0.15, 0.2, 0.2, 0.15, 0.1, 0.05, 0.03, 0.02])
trx_value
它似乎以一种不同的方式工作,因为结果没有显示我期望的值的唯一性:
> array([ 8.22611761, 8.22611761, 127.05325134, 8.22611761,
> 266.53172436, 35.43526485, 35.43526485, 88.57366253,
> 127.05325134, 12.38946805, 266.53172436, 127.05325134,
> 12.38946805, 35.43526485, 127.05325134, 12.38946805,
> 266.53172436, 8.22611761])
它似乎正在做这样的事情:
1: For each interval, pick a random number > Store this number in a pool
2: Pick a random number from the pool > Append number in array
...
n: Pick a random number from the pool > Append number in array
我确信这是一个思考问题,但我想知道在不使用for
循环的情况下这是否可能
转载请注明出处:http://www.fortunesungroup.com/article/20230330/1880964.html