Random Prime Number Generator (C++)

Search for a command to run...

No comments yet. Be the first to comment.
Transferring files between your local machine and a virtual machine (VM) is one of the most common tasks in tech. We often use scp because it comes natively installed and has similar utilization to ssh, but it is the clunkiest tool for the job. In th...

Alias Alchemy is a Deno-powered server that fetches aliases from a GitHub repository based on your query. Think of it as your instant alias spellbook, perfect for setting up new environments in seconds. ⚡ Usage: $ alias-alchemy.ra101.dev/?q=py,shell,...

Reader discretion is advised!A good Sus-Saturday to all. In this article, I drag and drown you with my paranoia about AI. Let the mass psychosis begin! What are AI hallucinations?AI hallucinations are incorrect or misleading results that AI models ge...

I recently joined a company, that follows squash-rebase flow here, and as my Git KT was going on, I figured I should build these commands before actually starting development, now that I have started development, these turned out to be quite handy! ...

Aim: To create an iterable blockchain, which could work with an extendable transaction class. Here the link GitHub repo! Blockchain in Brief: Blockchain is fundamentally a ledger maintaining records of transaction, This ledger is publicly distribute...

To create a random number engine that generates prime number on the go! Here the link GitHub repo
primeDB fileC++ already have random engines, Let us create a wrapper around that using templates() -> returns random prime numbermin() -> returns min prime numbermax() -> returns max prime number<< -> save the state to std::stringstream>> -> restores the state from std::stringstreamtemplate <typename Engine> Engine eng;int min_prime;int max_prime;char const *db_filepath;Constructorint operator()();friend stringstream &operator<<friend stringstream &operator>>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()
# 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
All the code can be found at the below link: https://github.com/ra101/Random-Prime-Number-Generator-Engine-cpp