简体   繁体   中英

How to get even/odd random number divisible by first random number

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.

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? ;-)

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.

You can do something like:

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

add this code at the bottom of your code.

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

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM