I have a question.

I'm writing a small program in c++ for my kid to help her with maths
and to teach myself how to program. I'm using srand() to get a few random numbers for her to divide with the number she choose.

All is fine until I get something like 5/3. She is in grade 3 and will not be able to do this. How can I make sure the random number are able to go into the numbers she chooses

void Divide::doRandDivide()
{
cout << "OK. Lest see if you understand" << "\n" << endl;
srand((unsigned)time(0));
int random_int;
int lowest = 1; 
int highest = 12;
int range=(highest-lowest)+1;
int index;
int iWrong; 
int irAns;
int irTotal;
int id3Total;
int iDeel;
for(index=0;index<5;index++)
{
random_int = lowest= int(range*rand()/(RAND_MAX + 1.0));
}
irTotal = idNommer/idNommer;
cout << "How much is " << idNumber << " divided by "<< idNumber << "\n" << endl;
cin >> irAns;
if (irAns != irTotal)
{
cout << "No this is wrong "<< "\n" << endl;
iWrong++;
if (iWrong >= 3)
{
cout << "\n" << endl;
cout << "The answer is " << irTotal << endl;
}
}
 
while (irAnw != irTotal);
{
cout << "Cool " << endl;
}
 
 
for(index=0;index<5;index++)
id3Total = random_int/idNumber;
cout << "How much is " << idNommer << " divided by "<< id3Total << "\n" << endl;
cin >> irAnw;
if (irAnw != id3Total)
{
cout << "No this is wrong "<< "\n" << endl;
iWrong++;
if (iWrong >= 3)
{
cout << "\n" << endl;
cout << "The answer is " << id3Total << endl;
}
}
else
cout << "Cool " << endl;
 
while (irAnt != id3Total);{}
}

Dani AI

Generated

Short diagnosis and what to do first

The simplest, most reliable way to guarantee an integer result is to build the dividend from the chosen divisor rather than picking a random dividend and hoping it divides. That avoids repeated retries and subtle RNG bugs. Two safe patterns are explained below; pick the one that best fits the size and variety of problems you want. Note a few common bugs in the original post: reseeding the RNG per call, accidental assignment to the wrong variable when computing a random value, stray semicolons after while that make the loop body empty, and using counters before they are initialized. Fix those first.

Two practical approaches

  • Preferred (fast and simple): choose a random multiplier, then compute dividend = divisor * multiplier. This guarantees an integer quotient every time and is perfect for grade‑3 problems (pick multiplier 1..12 to keep numbers small).
  • Alternative (random dividend distribution): sample random dividends and accept only those where dividend % divisor == 0, retrying until you find one. This gives a different distribution of dividends but requires a loop that must be guaranteed to terminate (ensure your search range contains multiples of the divisor).

Example (modern C++ PRNG, seed once)

#include <random>

std::mt19937 rng(std::random_device{}());

int make_dividend(int divisor, int max_multiplier = 12) {
    std::uniform_int_distribution<int> dist(1, max_multiplier);
    return divisor * dist(rng);
}

Extra tips

Mentioning earlier ideas from and : the retry/modulo idea and the multiply-to-create-dividend idea are both valid—use the multiply method for simplicity and speed. Seed your PRNG once (not every question), check divisor != 0, validate cin input (handle non-numeric input), and choose ranges so answers stay child-friendly.

Recommended Answers

All 2 Replies

I hevent gone thru your code ... You can do something like this

Random Number % Number chosen == 0
then
proceed
else
choose other random number.

Conceptual thought: start with two numbers and multiply them together. Then you have three numbers, one of which can be evenly divided by the other two. Display them as you choose to make the divide problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.