3

このコードを使用して、ブーストから UUID を生成します。

boost::uuids::random_generator gen;
boost::uuids::uuid uuidId = gen();
string randomUUID = boost::lexical_cast<std::string>(uuidId);
std::remove( randomUUID.begin(), randomUUID.end(), '-');
randomUUID = "0x" + randomUUID;

「0xCC5B9F6946EF4448A89EDB2042E0B084」のよ​​うな16進数が表示されます。

私の質問は次のとおりです。この文字列 (128 ビットの 16 進数) を 128 の long long または 64 bit long long に変換する方法 (より高いデータを失うことは問題ありません)。

この場合、標準の C 環礁と C++ std::stoll は役に立ちません。

ランダム生成の品質には UUID が優先されます。

ありがとうございました!

4

2 に答える 2

1

ランダムな 64 ビットの符号なし整数だけが必要な場合は、標準の C++11 を使用できます。

std::mt19937_64 engine(std::random_device{}());
std::uniform_int_distribution<uint64_t> distribution;
auto ui64 = distribution(engine);

ライブデモ

于 2016-04-29T12:41:18.250 に答える
0

このように動作しますが、エントロピーには調査が必要です:

typedef unsigned long long ull;
//...
ull ui64 = 0;
const int startPosition = 18;//can vary - we can use rand() too
const int lengthHex = 14;//can vary - we can use rand() too
boost::uuids::random_generator gen;
boost::uuids::uuid uuidId = gen();
string randomUUID = boost::lexical_cast<std::string>(uuidId);
std::remove( randomUUID.begin(), randomUUID.end(), '-');
randomUUID = "0x" + randomUUID.substr(startPosition, lengthHex);
ui64 = std::stoull(randomUUID, 0, 16); //random out of UUID
std::cout << ui64 << '\n';
于 2016-04-29T07:51:08.813 に答える