如何实现随机函数,其中每个后续值比前一个值更不可能?

时间:2014-08-31 21:35:52

标签: c++ random

为了进行比较,标准随机函数需要数字,让我们说3,那么这意味着012各有33%的机会回来。

我需要实现随机函数,让我们说0.5然后它意味着0有50%的机会返回,1是25%,{{1是12.5%,依此类推,直到无穷大。

我不能使用循环,例如:

2

因为当我将int SequencialRandom(double x) { int result=0; while (DoubleRandom()>x) //DoubleRandom() returns randomized double that ranges from 0.0 to 1.0. result++; return result; } 放在参数中时,平均会循环100次,而且性能不佳。这个问题有很好的算法吗?

2 个答案:

答案 0 :(得分:9)

您正在寻找的是几何分布,由std::geometric_distribution提供:

  

生成随机非负整数值i,根据离散概率函数分布:

     

P(i | p)= p·(1 - p) i

     

该值表示获得一次成功所必需的是/否试验(每次试验成功概率为p)的数量。

示例代码:

#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <random>
int main()
{
    std::random_device rd;
    std::seed_seq seed{r(), r(), r(), r(), r(), r(), r(), r()};
    std::mt19937 gen(seed);

    // same as std::negative_binomial_distribution<> d(1, 0.5);
    std::geometric_distribution<> d;

    std::map<int, int> hist;
    for(int n=0; n<10000; ++n) {
        ++hist[d(gen)];
    }
    for(auto p : hist) {
        std::cout << p.first <<
                ' ' << std::string(p.second/100, '*') << '\n';
    }
}

分配输出:

0 **************************************************
1 ************************
2 ************
3 ******
4 **
5 *
6 
7 
8 
9 
10 
13 

答案 1 :(得分:0)

int GeometericRandom(double distribution)
{
    return (int)floor(ln(DoubleRandom())/ln(distribution));
}

没有循环(复杂性只有O(1)),函数floor(ln(x)/ln(0.5)) 0<x<1的图如下所示:

enter image description here