TASK00 - Добавление функции генерации рандомного числа

This commit is contained in:
Антон
2025-06-12 11:24:46 +03:00
parent 412b83d4d1
commit e6ce15d422
3 changed files with 48 additions and 0 deletions
+10
View File
@@ -1,5 +1,6 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <random>
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <boost/format.hpp> #include <boost/format.hpp>
@@ -115,4 +116,13 @@ std::string ToHex(std::byte* src, size_t len)
return ret; return ret;
} }
uint64_t Random()
{
std::random_device device;
std::mt19937 random_generator(device());
std::uniform_int_distribution<std::mt19937::result_type> dist(0,UINT64_MAX);
return dist(random_generator);
}
} // namespace uad } // namespace uad
+2
View File
@@ -11,4 +11,6 @@ std::string PathCat(boost::beast::string_view base, boost::beast::string_view pa
void Fail(boost::beast::error_code ec, char const* what); void Fail(boost::beast::error_code ec, char const* what);
std::string ToHex(std::byte* src, size_t len); std::string ToHex(std::byte* src, size_t len);
uint64_t Random();
} // namespace uad } // namespace uad
+36
View File
@@ -52,3 +52,39 @@ BOOST_AUTO_TEST_CASE(ToHex_should_return_empty_string_if_no_arr_or_no_length)
BOOST_CHECK_EQUAL(ToHex(&a1, 0), ""s); BOOST_CHECK_EQUAL(ToHex(&a1, 0), ""s);
BOOST_CHECK_EQUAL(ToHex(nullptr, 1), ""s); BOOST_CHECK_EQUAL(ToHex(nullptr, 1), ""s);
} }
BOOST_AUTO_TEST_CASE(Random_should_return_different_numbers_on_multiple_calls)
{
auto a1 = Random();
auto a2 = Random();
auto a3 = Random();
auto a4 = Random();
auto a5 = Random();
auto a6 = Random();
auto a7 = Random();
auto a8 = Random();
auto a9 = Random();
auto a10 = Random();
auto b1 = Random();
auto b2 = Random();
auto b3 = Random();
auto b4 = Random();
auto b5 = Random();
auto b6 = Random();
auto b7 = Random();
auto b8 = Random();
auto b9 = Random();
auto b10 = Random();
BOOST_CHECK(a1 != b1);
BOOST_CHECK(a2 != b2);
BOOST_CHECK(a3 != b3);
BOOST_CHECK(a4 != b4);
BOOST_CHECK(a5 != b5);
BOOST_CHECK(a6 != b6);
BOOST_CHECK(a7 != b7);
BOOST_CHECK(a8 != b8);
BOOST_CHECK(a9 != b9);
BOOST_CHECK(a10 != b10);
}