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.
- 2Maybe you can generate two 64bit numbers, and conbine them into a 128bit number.Jun Ge– Jun Ge2016-12-10 13:07:10 +00:00Commented Dec 10, 2016 at 13:07
- Does the number still random generated in this way?@JunGeYjyJeff– YjyJeff2016-12-10 13:26:06 +00:00Commented Dec 10, 2016 at 13:26
Add a comment |
1 Answer
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; Targeting one of your comments earlier: yes this is a completely random 128 bit int, you just generate it in two steps.