This site uses strictly necessary cookies. More Information

X- Home /

**closed**Aug 29, 2014 at 05:35 PM by robertbu for the following reason:

Not a Unity Question

# Division Sum Generator problem

I've got a sum generator for an educational game I'm working on, I've got it working for addition, subtraction and multiplication but I'm having trouble coming up with a division sum generator, I need a way of making the second number a divisor of the first number, I'm just not sure how to go about doing that, any help is appreciated. Here's my existing code:

```
//Plus Problem Setup
void Plus()
{
Sign.text = "+";
number1 = Random.Range (1, 11);
number2 = Random.Range (1, 11);
answer = number1 + number2;
}
//Minus Problem Setup
void Minus()
{
Sign.text = "-";
number1 = Random.Range (1, 21);
number2 = Random.Range (1, number1);
answer = number1 - number2;
}
//Multiplication Problem Setup
void Multiplication()
{
Sign.text = "x";
number1 = Random.Range (1, 11);
number2 = Random.Range (1, 11);
answer = number1 * number2;
}
//Division Problem Setup
void Division()
{
Sign.text = "÷";
number1 = Random.Range (1, 21);
number2 = Random.Range (1, number1);
answer = number1 / number2;
}
```

Not a Unity Question. You can do it this way:

```
do {
number2 = Random.Range (1, number1+1);
} while (number1 % number2 != 0);
```

Note I'm assuming eventually this code will find '1' if the number is prime, but given computer Random, that may not be a sure thing. I always hesitate to put in any code that has the potential to hang. It might be better to make an attempt for some fixed number of trys, and if it fails, generate a new Random number:

```
int i = 0;
do {
i++;
if (i > 1000) {
number1 = Random.Rangle(1, 21);
i = 0;
}
number2 = Random.Range (1, number1+1);
} while (number1 % number2 != 0);
```

Note on the 'number1+1'. Unity Random.Range() is exclusive on the final value (i.e. it does not generate it) for integers. If I did not do that, and your number1 was assigned 1, then your code would hang.

I know this has been closed but just to point out, you could do this inversely by working out number1 based on a random pick of number2 and multiplying it by something, then you know for sure if divides to give 'something':

```
number2 = Random.Range (1, 21);
number1 = Random.Range (1, $$anonymous$$athf.CeilToInt(21f/(float)number2))*number2;
answer = number1/number2;
```

Scribe

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Random Room Generator 1 Answer

Code to randomly generate a mesh? 0 Answers

how can i make randomly generated worlds with collectable resources 0 Answers

Infinite loop when I try to generate randomly a 2D dungeon.. 3 Answers

How can I make a Terraria Style game from sprites without using an enormous amount of gameobjects? 2 Answers