基于不同因素的随机数生成
本文关键字:随机数 于不同 | 更新日期: 2023-09-27 18:14:30
我想写一个随机数生成算法。但我不希望这些数字完全是随机的。我希望它们取决于许多不同的因素。我说的因子是指值在0到1之间的变量。
所以因子可以是-
Bounce // 0.56 // Should afffect the outcome by 10% // inversely proportional to outcome
Spin // 0.71 // Should afffect the outcome by 20% // inversely proportional to outcome
Light // 0.99 // Should afffect the outcome by 5% // directly proportional to outcome
Crowd Support // 1.00 // Should afffect the outcome by 5% // directly proportional to outcome
Pressure // 0.89 // Should afffect the outcome by 10% // inversely proportional to outcome
Experience // 0.76 // Should afffect the outcome by 10% // directly proportional to outcome
Skill // 0.56 // Should afffect the outcome by 40% // directly proportional to outcome
现在基于这些因素,我想生成一个介于0-6之间的随机数或一个小柱。
我正在写这个算法来模拟板球比赛。
我可以用什么方法来写这样一个算法?
思路是分别计算各因素的随机系数。你会得到比结果特性更小的分布范围。
int max = 6;
var rand = new Random();
//to make code cleaner, returns 0..1 double
Func<double> next = () => rand.NextDouble();
double result = - Bounce * max * 0.1 * next()
- Spin * max * 0.2 * next()
+ Light * max * 0.05 * next() //0.1
+ CrowdSupport * max * 0.05 * next() //0.1
- Pressure * max * 0.1 * next()
+ Experience * max * 0.1 * next() //0.2
+ Skill * max * 0.4 * next(); //0.6
//calculated based on weights and negative'positive character
//result would be in range 0 and 6 with
另一个想法是分别计算正负因子,然后对所有因子应用随机系数,除以2,再加上最大值的一半。你会得到从0到6的随机分布。
double negativeFactor = Bounce * max * 0.25
+ Spin * max * 0.5
+ Pressure * max * 0.25;
double positiveFactor = Light * max * 0.1
+ CrowdSupport * max * 0.1
+ Experience * max * 0.2
+ Skill * max * 0.6;
double result = max / 2
+ positiveFactor * max * next() / 2
- negativeFactor * max * next() / 2;
正如Lasse V. Karlsen正确指出的那样,你需要以这样的方式为正因子选择权重,使它们的和为1。如果所有的负因子都为零,那么分布将包含六个最大值。我在源代码的注释中给出了这些因素的例子。
对于负面因素,您将允许它们将结果值降低到40%
。如果你想包含0作为结果,你也需要做这样的系数,这样它们的和将是1,示例也在注释