anon
anon

Reputation:

(Java) Generating random number for division program

Hi I'm new to programming and I'm trying to make a division program that can generate 2 random numbers and the condition is that the first number must be more than the second and they give no remainder. If the number generated does not meet the condition it keeps generating until the conditions are met. Can anyone help me fix my error?

randomnum1 = 1 + (int)(Math.random()*9);
randomnum2 = 1 + (int)(Math.random()*9);

while (randomnum1 < randomnum2 && randomnum1/randomnum2 % 2 != 0) {
            randomnum1 = 1 + (int)(Math.random()*9);
            randomnum2 = 1 + (int)(Math.random()*9);
            int number1 = randomnum1;
            int number2 = randomnum2;

        int a = number1/number2;
    
    //rest of program is below this

Upvotes: 1

Views: 810

Answers (4)

IQbrod
IQbrod

Reputation: 2265

Your while condition checks that the division result is even randomnum1/randomnum2 % 2 != 0
You should replace :

while (randomnum1 < randomnum2 && randomnum1/randomnum2 % 2 != 0) {

With

while (randomnum1 < randomnum2 || randomnum1 % randomnum2 != 0) {
// while (!(randomnum1 >= randomnum2 && randomnum1 % randomnum2 == 0)) {
    randomnum1 = 1 + (int)(Math.random()*9);
    randomnum2 = 1 + (int)(Math.random()*9);
}
// randomnum1 and randomnum2 now match your expectations
int a = number1/number2;

As rand1 modulo rand2 == 0 means that

they give no remainder

Upvotes: 1

Yahya
Yahya

Reputation: 14092

A better way, without having to use any loop, is to do some mathematical tricks as follows:

public class SpecialRandom{
    
    public void generate(){
        int first = 2 + (int) (Math.random() * 99);
        int second = 1 + (int) (Math.random() * 99);
        // to guarantee the second is always smaller
        if (second>=first){ second%=first; }
        if (second==0) { second++; }
        first += second - (first%second); //to  correct remainder
        System.out.println((first>second && first%second==0)
                            + " : " +first+ " ,  " +second);
    }
    
     /*TESTING*/
     public static void main(String []args){
       SpecialRandom sr = new SpecialRandom();
       for(int j=0; j<25; j++){ sr.generate(); }
     } 
}

Result

true : 28 ,  4
true : 64 ,  32
true : 22 ,  11
true : 18 ,  3
true : 28 ,  14
true : 18 ,  6
true : 92 ,  23
true : 96 ,  6
true : 130 ,  65
true : 28 ,  14
true : 87 ,  29
true : 87 ,  29
true : 74 ,  37
true : 112 ,  56
true : 66 ,  6
true : 10 ,  1
true : 88 ,  44
true : 68 ,  34
true : 156 ,  78
true : 22 ,  11
true : 95 ,  1
true : 86 ,  43
true : 14 ,  1
true : 82 ,  41
true : 98 ,  14

Upvotes: 1

tin
tin

Reputation: 68

      public static void main(String[] args) {
       int randomnum1=1 + (int)(Math.random()*99);
       int randomnum2=1 + (int)(Math.random()*99);
       

       while(randomnum1 % randomnum2 != 0 || randomnum1==randomnum2) {
           //prints first numbers generated 
           System.out.println(randomnum1+" "+randomnum2);
           randomnum1=1 + (int)(Math.random()*99);
           randomnum2=1 + (int)(Math.random()*99);
           
           }
       if (true) {
               //prints  numbers generated that made the statement true
           System.out.print("true :"+randomnum1+" "+randomnum2);
           }
       }
       
       
       }

Upvotes: 1

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79435

Another way of doing it can be by using an infinite loop and breaking the loop when the conditions are met.

public class Main {
    public static void main(String[] args) {
        int randomNum1, randomNum2;
        while (true) {// An infinite loop

            randomNum1 = 1 + (int) (Math.random() * 9);
            randomNum2 = 1 + (int) (Math.random() * 9);

            if (randomNum1 > randomNum2 && randomNum1 % randomNum2 == 0) {
                System.out.println(randomNum1 + " / " + randomNum2 + " = " + (randomNum1 / randomNum2));
                break;// Break the loop
            }
        }
    }
}

A sample run:

8 / 2 = 4

Upvotes: 1

Related Questions