Click here to Skip to main content
15,506,563 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, how can I set my WPF Application with random generator number for division without residue? I try to create a math game in which the user chooses the math operation he wants to calculate, then he chooses what level he wants. Addition, subtraction and multiplication are not a problem. The problem occurs during division.
I would like to see only numbers that have no remainder after division. I created the code shown below.

I would like to see only single-digit numbers that would be divided and the result must not be with the residue.
firstNumber would be: 1,2,3,4,5,6,7,8,9
secondNumber would be: 1(divides all),2(divides 2,4,6,8),3(divides 3,6,9),4(divides 4,8)
Wouldn't there be a more sophisticated solution?

What I have tried:

C#
int maxValue = 10;
Random number = new Random();
int total = 0;
int firstNumber = number.Next(1, maxValue);
int secondNumber = number.Next(1, maxValue);
if (firstNumber < secondNumber)
{
secondNumber = number.Next(1, firstNumber);
}
int residue = (firstNumber % secondNumber);
if (residue != 0)
{
firstNumber = firstNumber + 1;
}
else 
{
total = firstNumber / secondNumber;
}

I would like the values ​​to be single digits. This code still does not guarantee me without erroneous calculations.

Thank you for any advice
Posted
Updated 20-May-21 6:42am
v4
Comments
Richard MacCutchan 20-May-21 7:02am    
You have updated your question (still a number of typos in the code) but it is still not clear what result(s) you want.
Member 15170612 20-May-21 7:40am    
better now?
Richard MacCutchan 20-May-21 7:57am    
Sorry, no. What exactly are you trying to create?
Member 15170612 20-May-21 8:11am    
another try, i don't know how to explain it more
Richard MacCutchan 20-May-21 8:35am    
OK, I think i understand. So once you have the first number, you want a second number that will be divided exactly into the first, i.e. no remainder. The only way to guarantee that would be to use a loop, which checks every number up to the value of the first number. Something like:
for (secondNumber = 2; secondNumber <= firstNumber; secondNumber++)
{
    if (firstNumber % secondNumber == 0)
        break;
}
// second is now the selected value

I think you should start both sequences at 2 rather than 1.

A very approach is by construction:
  • Randomly generate the second (integer) number, say n2.
  • Randomly generate an integer factor, say f.
  • Let the first number, say n1, be equal to second number multiplied by the factor f (namely n1 = n2 * f)
 
Share this answer
 
Comments
Member 15170612 4-May-21 4:30am    
is there any way to ensure that the first and second numbers are single digits?
CPallini 4-May-21 4:48am    
Of course there is (maybe changing drastically the approach) but then, you have a very little freedom (1 divides all {1..9} range, 2 divides only {2,4,6,8}, 3 divides only {3,6,9}, 4 divides only {4,8}, all the other single digit numbers just divide themselves).
Member 15170612 4-May-21 4:56am    
when I set the factor to 1, the division result will always be one, so I would somehow like to combine it
Quote:
How can I set division without residue?

A division is:
dividend / divisor = quotient
Draw divisor and quotient, and multiply them to deduce the dividend.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900