C++11 随机数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14009637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:52:52  来源:igfitidea点击:

C++11 random numbers

c++randomc++11

提问by Baruch

I need to generate random numbers, but from as wide a range as possible (64 bit at least). I don't care if the distribution is perfect, so std::rand()would work, but it only returns an int. I understand that c++11 has some random number generating capability that can give any size number, but is very complex to use. Can someone post a simple example of how to use it as simply as possible to get the described functionality (64 bit or more random numbers) in as simple a way as possible (like std::rand())?

我需要生成随机数,但范围尽可能大(至少 64 位)。我不在乎分布是否完美,所以std::rand()会起作用,但它只返回一个int. 我知道 c++11 有一些随机数生成能力,可以给出任何大小的数字,但使用起来非常复杂。有人可以发布一个简单的示例,说明如何尽可能简单地使用它来以尽可能简单的方式(例如std::rand())获得所描述的功能(64 位或更多随机数)?

回答by jogojapan

This is how to use the C++11 random number generation for this purpose (adjusted from http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution):

这是为此目的使用 C++11 随机数生成的方法(从http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution调整):

#include <random>
#include <iostream>
int main()
{
  /* Initialise. Do this once (not for every
     random number). */
  std::random_device rd;
  std::mt19937_64 gen(rd());

  /* This is where you define the number generator for unsigned long long: */
  std::uniform_int_distribution<unsigned long long> dis;

  /* A few random numbers: */    
  for (int n=0; n<10; ++n)
    std::cout << dis(gen) << ' ';
  std::cout << std::endl;
  return 0;
}

Instead of unsigned long long, you could use std::uintmax_tfrom cstdintto get the largest possible integer range (without using an actual big-integer library).

代替unsigned long long,您可以使用std::uintmax_tfromcstdint来获得最大可能的整数范围(不使用实际的大整数库)。

回答by kennytm

We could easily wrap a random number generator engine into srand/rand-like methods like this:

我们可以很容易地将随机数生成器引擎包装成类似 srand/rand 的方法,如下所示:

#include <random>
#include <iostream>

struct MT19937 {
private:
    static std::mt19937_64 rng;
public:
    // This is equivalent to srand().
    static void seed(uint64_t new_seed = std::mt19937_64::default_seed) {
        rng.seed(new_seed);
    }

    // This is equivalent to rand().
    static uint64_t get() {
        return rng();
    }
};

std::mt19937_64 MT19937::rng;


int main() {
    MT19937::seed(/*put your seed here*/);

    for (int i = 0; i < 10; ++ i)
        std::cout << MT19937::get() << std::endl;
}

(Like srandand rand, this implementation does not care about thread-safety.)

(就像srandand rand,这个实现不关心线程安全。)

Well the wrapper functions are so trivial that you could just use the engine directly.

好吧,包装函数非常简单,您可以直接使用引擎。

#include <random>
#include <iostream>

static std::mt19937_64 rng;

int main() {
    rng.seed(/*put your seed here*/);

    for (int i = 0; i < 10; ++ i)
        std::cout << rng() << std::endl;
}

回答by RiaD

Not C++11, but easy enough

不是 C++11,但足够简单

((unsigned long long)rand() << 32) + rand()Here we generate two parts of int64 as int32's

((unsigned long long)rand() << 32) + rand()这里我们将int64的两部分生成为int32的

As JasonDpointed out, it assumes that rand()generate 32bit integer. It' spossible to xor rand() << x, rand() << (2*x), rand() << (3*x), etc, where x<= bit's in generate by rand()number`. It should be OK too.

正如所JasonD指出的,它假设rand()生成 32 位整数。它可以 xor rand() << xrand() << (2*x)rand() << (3*x)等,其中x<= 位在按rand()数字生成。也应该没问题。