【问题标题】:Make Random Numbers Tend / Average to a Specific Value使随机数趋于/平均到特定值
【发布时间】:2013-07-21 15:24:19
【问题描述】:

例如,我如何生成一个介于 0 和 1 之间的随机数列表,但让它们的平均值为 0.8?

我用 C++ 编写了这个小脚本,它会告诉你输出了哪些数字。不过,这个问题与 C++ 无关。

#include <iostream>
#include <random>
#include <time.h>
int main(int argCount, char** argVector) {
    std::cout << "Generating Randoms" << std::endl;

    float avarage = 0.F;
    srand(rand() + (int) time(NULL));
    float ceiling = 0;
    float bottom = 1;
    for(unsigned int i = 0; i < 1000000; i++) {
        float random = (float) (rand() % 101) / 100;
        if(random > ceiling)
            ceiling = random;
        else if(random < bottom)
            bottom = random;
        avarage += random;
    }
    std::cout << "Avarage: " << avarage/1000000 << std::endl;
    std::cout << "Ceiling: " << ceiling << std::endl;
    std::cout << "Bottom: " << bottom << std::endl;
    return 0;
}

这个输出:

Generating Randoms
Avarage: 0.499287
Ceiling: 1
Bottom: 0

我希望上限和下限仍为 0 和 1,但能够更改平均值。该算法最好也应该是有效的。

我现在再次发布 C++ 代码,但任何语言都可以。

【问题讨论】:

  • 您有首选的发行版吗?如果要设置mu,有很多选择。例如en.wikipedia.org/wiki/Normal_distribution。或者可能是均匀分布,或者 beta,或者......
  • 好吧,如果平均值是 0.5,就像现在一样,我希望所有数字都有尽可能多的机会被选择为 0.5。如果它像 0.7,我希望数字在接近 0.7 时有更多机会,以使平均结果为 0.7。所以它并不是那个 wiki 页面。
  • 我不确定我是否理解。您想要一个均值为 0.5 的均匀分布,但如果均值与 0.5 不同,则为非均匀分布?您需要指定接近 0.7 的数字的“机会”,在正态分布示例中,这基本上是方差参数。
  • 几率是 1% 几率的直线。如果您希望 0.70 为平均值,请将线稍微转一下,使 0.70 仍然是 1%,但它前面的数字会更少机会,而它前面的数字会更多。
  • 你能画出你的意思吗?请标记轴。

标签: c++ random numbers


【解决方案1】:

将您的数字提高到 .321928 次方将使平均值为 0.8,但范围仍为 0-1。

【讨论】:

  • 我如何计算这个数字?
  • 虽然会给您带来很多不准确之处。 (例如,当指向 0.2 时,平均为 0.3)
  • 0.2 时您使用了什么电源?应该是 2.32192
  • log(.2) / log(.5) = 2.32193
  • 在通电后仅通过将其转换为 0->1 值来修复它。双记录器=日志(70)/日志(50); float random = (float) pow((double) (rand() % 101), logger) / 100;
【解决方案2】:

NolanPower 有一个使用异能的好主意,但他推荐的选择异能的机制是关闭的。如果随机数U 是统一的(0,1)law of the unconscious statistician 表示我们可以将任何函数g(U) 的期望值推导出为Integral[g(U) from: 0 to: 1]。如果我们的函数g(U) 是多项式,即U**c 表示某个常数c,则评估积分会产生一般解1 / (c + 1) 作为预期值。将其设置为等于所需的平均值 m 并求解,我们得到 c = (1 / m) - 1

要获得 0.8 的预期值,c = (1 / 0.8) - 1 = 0.25,即,计算出U**0.25。要获得 0.2 的期望值,c = (1 / 0.2) - 1 = 4,即使用U**4 生成值。

【讨论】:

  • 感谢您在让我意识到自己不聪明之前让我感到聪明!
  • @NolanPower 嘿,你很聪明。基本方法非常聪明。只是分布不直观。
【解决方案3】:

这是一个生成标准正态分布的示例,即mu = 0,sigma = 1。

我用的是Box-Muller transform

所有地块都有x axis = valuey axis = frequency

#include <iostream>
#include <random>
#include <time.h>
#include <math.h>
int main(int argCount, char** argVector) {
    const double pi = 3.14159265359;
    const double nums = 1000000;
    double u, v, x;

    srand(rand() + (int) time(NULL));

    for(unsigned int i = 0; i < nums; i++){
        u = rand() / (((double)RAND_MAX) + 1.0);
        v = rand() / (((double)RAND_MAX) + 1.0);
        x = sqrt(-2*log(u)) * cos(2*pi*v);

        if (std::isfinite(x)){
            std::cout << x <<" ";
        }
    }

    return 0;
}

>>> np.std(nums)
1.0004139708929858
>>> np.average(nums)
7.1785002756408726e-05

您可以根据需要移动/缩放x 以获得您选择的musigma

这里有一个例子,给出了一个给定mu 的均匀分布:

#include <iostream>
#include <random>
#include <time.h>
#include <math.h>
int main(int argCount, char** argVector) {
    const double pi = 3.14159265359;
    const double nums = 1000000;
    double x,mu;

    srand(rand() + (int) time(NULL));
    mu = 3.0;

    for(unsigned int i = 0; i < nums; i++){
        x = rand() / (((double)RAND_MAX) + 1.0);
        x *= 2*mu;

        if (std::isfinite(x)){
            std::cout << x <<" ";
        }
    }

    return 0;
}

>>> np.average(nums)
3.0003091558133184

您可以使用documented rand() % range + min 进行截断。

【讨论】:

    猜你喜欢
    • 2013-07-07
    • 2013-03-07
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多