From 3fca6809b5d7e677fd46a60a365cbeba402d9d80 Mon Sep 17 00:00:00 2001 From: Aitik Gupta Date: Wed, 16 Oct 2019 04:21:15 +0530 Subject: [PATCH] Added big_random function => within a certain range --- include/functions/random.hpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/include/functions/random.hpp b/include/functions/random.hpp index 10d08cc..2e61b2b 100644 --- a/include/functions/random.hpp +++ b/include/functions/random.hpp @@ -45,4 +45,24 @@ BigInt big_random(size_t num_digits = 0) { } +/* + big_random (low, high) + ----------------------- + Returns a random BigInt such that low <= BigInt <= high +*/ + +BigInt big_random(size_t low = 0, size_t high) { + std::random_device rand_generator; // true random number generator + + num_digits = 1 + rand_generator() % high.value.size(); + + BigInt big_rand; + big_rand.value = ""; // clear value to append digits + + while (big_rand.value > low && big_rand.value < high) + big_rand.value += std::to_string(rand_generator()); + + return big_rand; +} + #endif // BIG_INT_RANDOM_FUNCTIONS_HPP