1

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.

2
  • 2
    Maybe you can generate two 64bit numbers, and conbine them into a 128bit number. Commented Dec 10, 2016 at 13:07
  • Does the number still random generated in this way?@JunGe Commented Dec 10, 2016 at 13:26

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.