【问题标题】:Uniform distribution using C++ [duplicate]使用 C++ 进行均匀分布 [重复]
【发布时间】:2014-10-24 23:57:32
【问题描述】:

我有两个变量 RS。我将它们定义为int R[1000]int S[100000]R 将保存从 0999 的值。现在,我希望 R 值均匀分布在 S100000 值)中。 S中的条目数应该是100000,但它们应该均匀分布在0-999的范围内。

我该怎么做?

【问题讨论】:

  • 这里不能代表其他所有人,但我个人先写一些代码。
  • std::uniform_int_distribution
  • @CaptainObvlious 是的,即使我不需要确切的代码,你能不能给我一些想法。
  • std::uniform_int_distribution from <random> 自 C++11 起?
  • 是的。但是 uniform_int_distribution 是 std 的成员吗???

标签: c++


【解决方案1】:

您可以使用 C++11 STL 编写类似以下内容来快速填充 RS

#include <numeric>
#include <random>

int main() {
    int R[1000], S[100000];
    std::iota( std::begin( R ), std::end( R ), 0 );
    std::mt19937 engine;
    std::uniform_int_distribution<int> dist( 0, 999 );
    std::generate( std::begin( S ), std::end( S ), []{ return dist( engine ); } );
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-14
    • 2012-07-10
    • 2011-04-04
    • 1970-01-01
    • 2021-12-28
    • 2013-01-05
    • 1970-01-01
    • 2011-09-16
    相关资源
    最近更新 更多