Random-Prime-Number-Generator-Engine-cpp

namespace ra :: Random Prime Generator

Aim:

To create a random number engine that generates prime number on the go!

Plan of Attack:

Objective:

Code:

Constructor: takes in seed and filepath

#define DB_FILEPATH "primeDB"

template <typename Engine>
random_prime_engine<Engine>::random_prime_engine(unsigned long rand_seed, char const *db_filepath=DB_FILEPATH)
{
    // Get DB path
    this->db_filepath = db_filepath;
    eng.seed(rand_seed);
}
// Usage

ra::random_prime_engine<std::default_random_engine> rpe_2(rand_seed);

() operator,

#define DB_SIZE 10000

template <typename Engine>
int random_prime_engine<Engine>::operator()()
{
    int idx = eng() % DB_SIZE; // to get a index within file
    int rand_prime;

    // Open the file
    std::ifstream file;
    file.open(db_filepath);

    // update rand_prime till it gets the value at index
    for (int i = 0; i <= idx; i++)
        file >> rand_prime;

    file.close();
    return (rand_prime);
}
// Usage

std::cout << rpe()

min and max

# pseudo code
# initialize min/max with 0, for lazy initialization

{
    # return min or max if once found
    if bool(min/max):
        return min/max

    min = file.first_line
    max = file.last_line

    return min/max
}

>> and <<

// Friend Functions
#define stringstream SS
function SS &operator<>(SS&in/out, rpe){
    in/out >> rpe.eng;
    return in/out
}
// Usage

std::stringstream state;
state << rpe; //save current state
state >> rpe; //restore old state