【问题标题】:Random number generation based on different factors [duplicate]基于不同因素的随机数生成[重复]
【发布时间】:2013-05-14 01:48:14
【问题描述】:

我想写一个随机数生成算法。但我不希望这些数字是完全随机的。我希望它们依赖于许多不同的因素。我所说的因子是指值在 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 之间的随机数或一个检票口。

我正在编写这个算法来模拟板球比赛。

我可以采取什么方法来编写这样的算法?

【问题讨论】:

  • 变量如何改变结果?值 1 而不是值 0.5,这是什么意思?平均随机结果更高?
  • 代码无关紧要。问题是纯数学。你有不同的因素,但你想根据它们计算什么?它们之间有什么关系?
  • @LasseV.Karlsen 因因素而异,技能越高得分越高,压力越大得分越低。
  • @AnkurSharma 问题是没有足够的细节,这是一个讨论类型的问题,因为你需要和某人坐下来进行实验,讨论你得到的结果,修改算法或公式,进行更多实验等。这不是问答问题。公式只有在你计算出来之后才会存在,所以你不能只是要求别人给你。没有人拥有它。
  • @LasseV.Karlsen 同意。我需要用我自己的大脑。感谢您的意见。

标签: c# algorithm random


【解决方案1】:

想法是单独计算所有因素的随机系数。您将在结果特征上获得较低的分布范围。

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。如果所有负面因素都为零,那么分布将包括 6 个最大值。我在源代码的 cmets 中给出了这些因素的示例。

对于负面因素,您将允许它们将结果值减小到 40%。如果你想包含 0 作为结果,你还需要使这样的系数,使它们的总和为 1,示例也在 cmets 上

【讨论】:

  • 这怎么会产生 6 的值(或接近它)?如果所有负变量的值都为 0,所有的正值都为 1,并且所有随机值都返回 1,那么你仍然只能得到 3.6。
  • @LasseV.Karlsen 好笔记。积极因素的权重总和应为 1。更正了我的答案。现在,如果您在 cmets 中替换因子(我确实想保留原始 OP 权重),您将得到 6 作为结果
  • 哇。谢谢。这很棒。我会修改它以适应我的需要。你知道是否有办法返回检票口(可能是 -1 或 7 的值)。我将如何修改这个函数来做到这一点?
  • @AnkurSharma 你说的检票口是什么意思?你能描述它,包括例子吗?是2D 数组吗?
  • Wicket 是板球运动的一部分(很像棒球),我试图用这个算法来模拟它。您可以得分 0-6 分,也可以被称为检票口的守场员抓住。
猜你喜欢
  • 2014-04-29
  • 1970-01-01
  • 1970-01-01
  • 2012-01-23
  • 1970-01-01
  • 2019-05-21
相关资源
最近更新 更多