我想从三个列表中随机选择一个项目,每个列表代表不同的选择概率。
我有三个列表(也可以是3个数组):高,中,低“优先级”。
我想按优先级从这三个列表中选择一项
例如,从高70%的概率从中20%到低10%的概率
但是,其中一些列表可能是空的(不是全部)
其中一个列表中至少有一项
我正在寻找能够做到这一点的算法(任何语言,但更喜欢C# Java Python
我尝试了以下代码(Python),但由于条件为空,它不能完成工作,有时即使有一项也没有选择。
x = random.randint(1,100)
if x < 71 and highChance != []:
return random.choice(highChance)
elif x >=71 and x < 91 and midChance != []:
return random.choice(midChance)
elif lowChance != []:
return random.choice(lowChance)发布于 2014-08-10 10:24:05
int[] high = new int[] { 0, 1, 2, 3 };
int[] mid = new int[] { 0, 1 };
int[] low = new int[] { };int[][] arr = new int[3][];
arr[0] = high.Length != 0? high: mid.Length != 0? mid: low;
arr[1] = mid.Length != 0 ? mid : arr[0];
arr[2] = low.Length != 0 ? low : arr[0];
Random rnd = new Random(0);
int n = rnd.Next(10);
int k = (n < 7) ? 0 : (n < 9) ? 1 : 2;
int result = arr[k][rnd.Next(arr[k].Length)];https://stackoverflow.com/questions/25223970
复制相似问题