通过boost :: random在c ++中随机生成128位(uint128_t)

时间:2016-12-10 12:20:47

标签: c++ boost random

如何通过boost :: random在c ++中生成128位数。我已经阅读了官方文件。 uniform_int_distribution只能生成64位(int的长度)。 提前谢谢。

1 个答案:

答案 0 :(得分:2)

正如Jun Ge所说,你可以将两个64位整数组合成一个128位整数。 C ++规范不包括128位整数,因此您需要找到另一种方法。也许你的编译器有它们。你使用boost :: random所以我假设你可以使用boost :: multiprecision。

#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;

int128_t int64left = CreateRandomInt64ViaBoost()
int128_t int64right = CreateRandomInt64ViaBoost()

int128_t randomInt = int64left << 64 | int64right;

之前针对您的一条评论:是的,这是一个完全随机的128位int,您只需分两步生成它。