Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

big_random function: fixed description and improved implementation #49

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<img alt="BigInt" src="logo.png">
</p>
<h3 align="center">Arbitrary-sized integer class for C++</h3>

---

[![Release version][release-shield]][release-link]
Expand Down Expand Up @@ -200,14 +199,14 @@

* #### Random
* #### `big_random`
Get a random `BigInt`, that either has a random number of digits (up to
Get a random positive `BigInt`, that either has a random number of digits (up to
1000), or a specific number of digits.

```c++
// get a random BigInt that has a random number of digits (up to 1000):
// get a random postive BigInt that has a random number of digits (up to 1000):
big1 = big_random();

// get a random BigInt that has 12345 digits:
// get a random positive BigInt that has 12345 digits:
big1 = big_random(12345);
```

Expand Down
43 changes: 28 additions & 15 deletions include/functions/random.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,42 @@ const size_t MAX_RANDOM_LENGTH = 1000;
/*
big_random (num_digits)
-----------------------
Returns a random BigInt with a specific number of digits.
Returns a random positive BigInt with a specific number of digits.
*/

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
/**
* This generators needed to be created only once,
* because properly seeded std::mt19937 can generate
* 2^19937 - 1 potential states which is more than
* enough for most usecases
*/

// seed sequence, seeded with true random generator
// used for better entropy
static std::seed_seq sseq{std::random_device{}()};
// generator itself
static std::mt19937 gen{sseq};
// different purpose distributions(cheap to create and use)
static std::uniform_int_distribution<> digit_distr(0, 9);
static std::uniform_int_distribution<> first_digit_distr(0, 9);
static std::uniform_int_distribution<> length_distribution(1, MAX_RANDOM_LENGTH);
// number to generate
static BigInt big_rand;

// first digit
big_rand.value = std::to_string(first_digit_distr(gen));

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

// ensure that the first digit is non-zero
big_rand.value += std::to_string(1 + rand_generator() % 9);
num_digits = length_distribution(gen);

// insert other digits
while (big_rand.value.size() < num_digits)
big_rand.value += std::to_string(rand_generator());
if (big_rand.value.size() != num_digits)
big_rand.value.erase(num_digits); // erase extra digits
big_rand.value += std::to_string(digit_distr(gen));

return big_rand;
}


#endif // BIG_INT_RANDOM_FUNCTIONS_HPP