有三个变量满足
p 1 + p 2 + p 3 = n p_1+p_2+p_3=n p1+p2+p3=n
a 1 < p 1 < b 1 a_1<p_1<b_1 a1<p1<b1
a 2 < p 2 < b 2 a_2<p_2<b_2 a2<p2<b2
a 3 < p 3 < b 3 a_3<p_3<b_3 a3<p3<b3
则思路:
- step 1:
a 3 < p 3 < b 3 a_3<p_3<b_3 a3<p3<b3,
根据 p 1 + p 2 + p 3 = n p_1+p_2+p_3=n p1+p2+p3=n,
a 3 < n − p 1 − p 2 < b 3 a_3<n-p_1-p_2<b_3 a3<n−p1−p2<b3
n − ( b 3 + p 1 ) < p 2 < n − ( a 3 + p 1 ) n-(b_3+p_1)<p_2<n-(a_3+p_1) n−(b3+p1)<p2<n−(a3+p1)
- step 2:
由于 a 2 < p 2 < b 2 a_2<p_2<b_2 a2<p2<b2
综上 a 2 ≤ n − ( a 3 + p 1 ) a_2\leq n-(a_3+p_1) a2≤n−(a3+p1)
而且 b 2 ≥ n − ( b 3 + p 1 ) b_2\geq n-(b_3+p_1) b2≥n−(b3+p1)
可得
n − ( a 2 + a 3 ) ≤ p 1 ≤ n − ( b 2 + b 3 ) n-(a_2+a_3)\leq p_1\leq n -(b_2+b_3) n−(a2+a3)≤p1≤n−(b2+b3)
结合 a 1 < p 1 < b 1 a_1<p_1<b_1 a1<p1<b1
可先随机取满足条件的 p 1 p_1 p1的值
- step 3:
然后再递推回去得到满足条件的 p 2 p_2 p2的值,进而可以根据 p 1 + p 2 + p 3 = 1 p_1+p_2+p_3=1 p1+p2+p3=1确定 p 3 p_3 p3的值
Python实现:
#%%
import random
def g(x_low, x_up, x_sum, y):
if len(x_low) == 1:
y.append(x_sum)
print(x_sum)
else:
a = max(x_sum-sum(x_up[1:len(x_up)]), x_low[0])
b = min(x_sum-sum(x_low[1:len(x_low)]), x_up[0])
temp = random.uniform(a,b)
y.append(temp)
print(temp)
x_low = x_low[1:len(x_low)]
x_up = x_up[1:len(x_up)]
x_sum = x_sum - temp
g(x_low, x_up, x_sum, y)
#return y
x_low = [0, 0, 0] #即
a
1
,
a
2
,
a
3
=
0
a_1,a_2,a_3 = 0
a1,a2,a3=0
x_up = [1, 1, 1] #即
b
1
,
b
2
,
b
3
=
1
b_1,b_2,b_3 = 1
b1,b2,b3=1
x_sum =1 #即
p
1
+
p
2
+
p
3
=
1
p_1+p_2+p_3=1
p1+p2+p3=1
y = []
Y = []
i=0
g(x_low, x_up, x_sum, y)