通过boost :: random在C ++中随机生成128位(uint128_t) [英] Generate 128 bit(uint128_t) randomly in c++ via boost::random
问题描述
如何在C ++中通过boost :: random生成一个128位数字.我已经阅读了官方文件. Uniform_int_distribution只能生成64位(int的长度). 预先感谢.
How to generate a 128 bit number in c++ via boost::random. I have read the official doc. The uniform_int_distribution could only generate 64 bit(length of int). Thanks in advance.
推荐答案
正如Jun Ge所说,您可以将两个64位整数组合成一个128位整数. C ++规范不包含128位整数,因此您需要找到另一种方法.也许您的编译器拥有它们.您使用boost :: random,所以我假设您可以使用boost :: multiprecision.
As Jun Ge said you could combine two 64 bit integers into a 128 bit integer. The C++ spec does not include 128 bit integers, so you need to find an alternative way. Maybe your compilers has them. You use boost::random so I assume you can use 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,只需分两步即可生成它.
Targeting one of your comments earlier: yes this is a completely random 128 bit int, you just generate it in two steps.
这篇关于通过boost :: random在C ++中随机生成128位(uint128_t)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!