Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Random functions for long long, BigInt, and std::string using specialized Template functions.
  • Loading branch information
stkaufer committed Oct 5, 2018
1 parent 5e3a130 commit 3279ec8
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 3 deletions.
2 changes: 2 additions & 0 deletions include/BigInt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ class BigInt {

// Random number generating functions:
friend BigInt big_random(size_t);
template <class T>
friend BigInt big_random(const T& low, const T& high);
};

#endif // BIG_INT_HPP
72 changes: 69 additions & 3 deletions include/functions/random.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,85 @@ const size_t MAX_RANDOM_LENGTH = 1000;
BigInt big_random(size_t num_digits = 0) {
std::random_device rand_generator; // true random number generator

if (num_digits == 0) // the number of digits were not specified
if (num_digits == 0) { // the number of digits were not specified
// use a random number for it:
num_digits = 1 + rand_generator() % MAX_RANDOM_LENGTH;
}

BigInt big_rand;
big_rand.value = ""; // clear value to append digits
while (big_rand.value.size() < num_digits)
while (big_rand.value.size() < num_digits) {
big_rand.value += std::to_string(rand_generator());
if (big_rand.value.size() != num_digits)
}
if (big_rand.value.size() != num_digits) {
big_rand.value.erase(num_digits); // erase extra digits
}

return big_rand;
}

static long long ll_digits_count(long long number) {
long long num_digits = 0;
while (number) {
number /= 10;
num_digits++;
}
return num_digits;
}

template <class T>
BigInt big_random(const T& low, const T& high) {
BigInt result;
size_t num_low_digits = 0;
size_t num_high_digits = 0;

num_low_digits = digits_count(low);
num_high_digits = digits_count(high);

result = low + big_random((num_low_digits < num_high_digits ? num_high_digits : num_low_digits))%(high - low);
return result;
}

template <>
BigInt big_random(const BigInt &low, const BigInt &high) {
BigInt result;
BigInt bi_random;
size_t num_low_digits = 0;
size_t num_high_digits = 0;

num_low_digits = ll_digits_count(low.to_long_long());
num_high_digits = ll_digits_count(high.to_long_long());

result = stol(low.value) + stol(big_random((num_low_digits < num_high_digits ? num_high_digits : num_low_digits)).value) % (stol(high.value) - stol(low.value));
return result;
}

template <>
BigInt big_random(const std::string& low, const std::string& high) {
BigInt result = 0;
size_t num_low_digits = 0;
size_t num_high_digits = 0;

long long ll_low = std::stol(low);
long long ll_high = std::stol(high);
num_low_digits = ll_digits_count(ll_low);
num_high_digits = ll_digits_count(ll_high);

result = std::stol(low) + big_random((num_low_digits < num_high_digits ? num_high_digits : num_low_digits)).to_long() % (std::stol(high) - std::stol(low));
return result;
}

template <>
BigInt big_random(const long long &low, const long long &high) {
BigInt result = 0;
size_t num_low_digits = 0;
size_t num_high_digits = 0;

num_low_digits = ll_digits_count(low);
num_high_digits = ll_digits_count(high);

result = low + big_random((num_low_digits < num_high_digits ? num_high_digits : num_low_digits)).to_long() % (high - low);
return result;
}

#endif // BIG_INT_RANDOM_FUNCTIONS_HPP
22 changes: 22 additions & 0 deletions test/functions/random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,25 @@ TEST_CASE("Generate random BigInts having a specified number of digits",
REQUIRE(big_random(num_digits).to_string().size() == num_digits);
}
}

TEST_CASE("Generate random BigInts of different types",
"[functions][random]") {
long long ll_zero = 0;
long long ll_min = LLONG_MIN;
long long ll_max = LLONG_MAX;
BigInt bi_zero = 0;
BigInt bi_min = LLONG_MIN;
BigInt bi_max = LLONG_MAX;
std::string s_zero = std::to_string(0);
std::string s_min = std::to_string(LLONG_MIN);
std::string s_max = std::to_string(LLONG_MAX);
REQUIRE(big_random(ll_zero, ll_max).to_string().size() <= 19);
REQUIRE(big_random(ll_min, ll_max).to_string().size() <= 21);

REQUIRE(big_random(bi_zero, bi_max).to_string().size() <= 19);
// This test always fails
//REQUIRE(big_random(bi_min, bi_max).to_string().size() >= 0);

REQUIRE(big_random(s_zero, s_max).to_string().size() <= 19);
REQUIRE(big_random(s_min, s_max).to_string().size() <= 21);
}

0 comments on commit 3279ec8

Please sign in to comment.