0

I have following code:

        quesPart1 = ran.nextInt((numbersBetween - 2) + 1) + 2;
        quesPart2 = ran.nextInt((numbersBetween - 2) + 1) + 2;
        if(quesPart2 > quesPart1)
        {
            int placeHolder = quesPart1;
            quesPart1 = quesPart2;
            quesPart2 = placeHolder;                    
        }
        //if first part is even
        if(quesPart1 % 2 == 0)
        {
            if(quesPart2  % 2 != 0)
            {
                --quesPart2;
            }
        }
        else
        {
            if(quesPart2 % 2 == 0)
            {
                ++quesPart2;
            }
        }

Above code make sure that if quesPart1 is greater than quesPart2 and both are even or both are odd numbers. Now i want to get only random numbers which are also divisible by one another. Like if i divide quesPart1 by quesPart2 i get integer not decimal number. Any ideas how i can do that without adding too much complexity to above code.

NoviceMe
  • 3,126
  • 11
  • 57
  • 117

5 Answers5

1

Like if i divide quesPart1 by quesPart2 i get integer not decimal number.

Keep it simple: generate random numbers and take their product. Example:

quesPart2 = ran.nextInt(UPPER_BOUND);
int temp = ran.nextInt(UPPER_BOUND);
questPart1 = temp * quesPart2;

Specifying the range, as in the original question, is left an an exercise to the reader. (What, you didn't think I was going to do all the thinking for you, did you? ;-)

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • Still trying to understand your answer. Lets say i do this: ran.nextInt((10 - 2) + 1) + 2; and get quesPart2 = 5 and do same for temp and get temp as 5. Than quesPart1 will be 25 which is out of the range i specified? – NoviceMe Nov 14 '13 at 04:59
  • Set the upper bound when generating `temp`, based on the value of `quesPart2`, such that `temp * quesPart2` cannot be out of range `;-)` – Matt Ball Nov 14 '13 at 05:03
  • If UPPER_BOUND is 10 in above case. I will always get 2/2, 3/3,4/4, 5/5... so on following above code which does not seem right? – NoviceMe Nov 14 '13 at 05:33
  • I mean you should use different upper bounds for the two `nextInt()` calls. – Matt Ball Nov 14 '13 at 12:37
1

Look into the modulus operator, a % b. It returns the left over amount when a is divided by b. When b cleanly divides into a, such that there is no decimal part, a % b will be zero.

In order to generate a number that is divisible by another, given two random numbers, a and b, simply multiply a by b. This will give you c, a number that is a multiple of both a and b, and therefore dividable by both cleanly without remainder.

enalicho
  • 570
  • 3
  • 6
1

You can do something like:

int div = quesPart1 / quesPart2;
quesPart1 = div * quesPart2;

add this code at the bottom of your code.

rcs
  • 6,713
  • 12
  • 53
  • 75
  • In above case if we get quesPart1 = 5 and quesPart2 = 3 it is not working. Like these there are many more combinations which go through the loop and does not give right result. – NoviceMe Nov 14 '13 at 05:06
  • If `quesPart1 = 5` and `quesPart2 = 3`, then in the end `quesPart1 = 3` which is divisible by `quesPart2`. Why is it not correct? – rcs Nov 14 '13 at 05:13
0

I have come up with this simple function and a do while loop that is easy to implement.

// This is a simple function to set the min and max integers you want
    const getRandomIntInclusive = (min, max) => {
          min = Math.ceil(min);
          max = Math.floor(max);
          return Math.floor(Math.random() * (max - min + 1)) + min;
        }
        //Define some variables variables
        let firstNo = 0
        let secondNo = 0
        let isDivisible = 0;
        //generate random ints until first number is divisible to second number
        do {
           //get random int between 1-9 for the first and second integer
           firstNo = getRandomIntInclusive(1, 9) 
           secondNo = getRandomIntInclusive(1, 9)
           isDivisible = firstNo % secondNo;  //Check if it's fully divisible
           }
           while (isDivisible != 0)      //Run until it is fully divisible
Waweru Kamau
  • 1,429
  • 14
  • 20
-1

To generate Random numbers in java you can use ran.nextInt() or please refer to this link to see how to generate random numbers. store those 2 random numbers (as num1 and num2).

To verify whether the solution after dividing num1 and num2 is integer or not, use this method:

sol = num1 / num2
if (sol == (int)sol)
{
   ... //true if the solution is an integer
}
Community
  • 1
  • 1
s.mujahid8
  • 189
  • 3
  • 6
  • 15
  • @downvoter: i would like to get some comments for voting the answer down, so that i can improve myself and give better solutions in future. – s.mujahid8 Nov 13 '13 at 06:27
  • This does not answer the question that the OP asked. They clearly know how to generate random numbers. As with @enalicho's answer, your answer shows how to check the divisibility, but that's now that the OP is asking. Even worse than @enalicho's answer, though, is that your divisibility check doesn't use `%`. – Matt Ball Nov 13 '13 at 13:32
  • i agree this method doesnt generate such numbers which exactly divide each other. but its practically impossible to generate 2 random numbers which divide perfectly, giving integer solution unless you multiply the 2 random numbers. This method just checks the final solution if the remainder is an integer or decimal It doesnt require `%`. The if condition will take care of verifying the remainder. – s.mujahid8 Nov 14 '13 at 04:22