В C++ существует несколько методов генерации случайных целых чисел в 64-битной системе. Вот несколько подходов:
-
Использование библиотеки
:#include <random> std::random_device rd; std::mt19937_64 generator(rd()); // Generate a random 64-bit integer uint64_t randomInt = generator(); -
Использование
std::rand()и битового сдвига:#include <cstdlib> // Generate a random 64-bit integer uint64_t randomInt = ((uint64_t)std::rand() << 32) | std::rand(); -
Использование библиотеки
C++11:#include <random> std::random_device rd; std::mt19937_64 generator(rd()); std::uniform_int_distribution<uint64_t> distribution(0, std::numeric_limits<uint64_t>::max()); // Generate a random 64-bit integer uint64_t randomInt = distribution(generator); -
Использование библиотеки
C++11 сrandom_deviceв качестве начального значения:#include <random> std::random_device rd; std::seed_seq seed{ rd(), static_cast<unsigned int>(time(nullptr)) }; std::mt19937_64 generator(seed()); std::uniform_int_distribution<uint64_t> distribution(0, std::numeric_limits<uint64_t>::max()); // Generate a random 64-bit integer uint64_t randomInt = distribution(generator); -
Использование библиотеки
C++17 сstd::random_deviceв качестве начального значения:#include <random> std::random_device rd; std::seed_seq seed{ rd(), static_cast<unsigned int>(time(nullptr)) }; std::mt19937_64 generator(seed); std::uniform_int_distribution<uint64_t> distribution(0, std::numeric_limits<uint64_t>::max()); // Generate a random 64-bit integer uint64_t randomInt = distribution(generator);