-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathrandom.hpp
More file actions
43 lines (31 loc) · 1.22 KB
/
Copy pathrandom.hpp
File metadata and controls
43 lines (31 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
===========================================================================
Random number generating functions for BigInt
===========================================================================
*/
#ifndef BIG_INT_RANDOM_FUNCTIONS_HPP
#define BIG_INT_RANDOM_FUNCTIONS_HPP
#include <random>
#include <climits>
#include "BigInt.hpp"
// when the number of digits are not specified, a random value is used for it
// which is kept below the following:
const size_t MAX_RANDOM_LENGTH = 1000;
/*
big_random (num_digits)
-----------------------
Returns a random 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
// use a random number for it:
num_digits = 1 + rand_generator() % MAX_RANDOM_LENGTH;
std::string random_value = "";
while (random_value.size() < num_digits)
random_value += std::to_string(rand_generator());
if (random_value.size() != num_digits)
random_value.erase(num_digits); // erase extra digits
return BigInt(random_value);
}
#endif // BIG_INT_RANDOM_FUNCTIONS_HPP