2

I am implementing a math table using two integers (a and tableSize). I have built a random operation named R. I am going to calculate a random number between the row and column range and print the random number. For those instances where the row value is larger than the column value, the output is a dash ("-").

Here is my code,

    int a = 4;
    int tableSize = 10;
    System.out.print("    R ");
    for(int i = a; i <= tableSize;i++ )
    {
        System.out.format("%4d",i);
    }   
    System.out.println();
    for(int i = a ;i <= tableSize;i++)
    {
        System.out.format("%4d ",i);
        for(int j=a;j <= tableSize;j++)
        {
            int randomNum = rand.nextInt (j) + i;
            if(!(i > j))
            {
                System.out.format("%4d", randomNum);
            } else
            {
                System.out.format("%4s", "-");
            }
         }
         System.out.println();
     }

The output I need is like this,

R  4  5  6  7  8  9  10
4  4  4  5  5  4  9   8
5  -  5  5  6  5  9   8
6  -  -  6  6  7  9   6
7  -  -  -  7  7  7   7
8  -  -  -  -  8  9   9
9  -  -  -  -  -  9  10
10 -  -  -  -  -  -  10

But the problem is I didn't get output like that. output I receive is,

   R    4   5   6   7   8   9  10
   4    5   7   6   8   8  10  13
   5    -   5   9   8   8  10  12
   6    -   -   9   8  11  10  11
   7    -   -   -   8  14   9  16
   8    -   -   -   -  14  12  11
   9    -   -   -   -   -  13  18
  10    -   -   -   -   -   -  19

And the row value is larger than the column value, Please anyone can help me? Thanks in advance.

bob
  • 4,282
  • 2
  • 22
  • 30
Hasitha Jayawardana
  • 2,326
  • 4
  • 18
  • 36

4 Answers4

3

The problem is that you are computing the cell value as the sum of a random number between 1 and the column number plus the row number. The logic I think you want is that a given cell in the matrix can be no larger than the max of the row or column number. If so, then you need to change this line:

int randomNum = rand.nextInt(j) + i;

To this:

int randomNum = rand.nextInt((Math.max(i, j) - a) + 1) + a;

Demo

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
3

Change your random number like this.

 int randomNum = rand.nextInt((tableSize - a) +1)+a;

Output:

   R    4   5   6   7   8   9  10
   4    4   6   7   6   6   7   7
   5    -   6   5   4   6   8   8
   6    -   -   7   8   7   7   8
   7    -   -   -  10   7   5   5
   8    -   -   -   -   9   5   8
   9    -   -   -   -   -   8   8
  10    -   -   -   -   -   -   4
Exteam
  • 63
  • 7
3

You want a number that can be up to the higher limit (inclusive), but Random.nextInt(int) excludes the higher limit, so you need to add 1 to the argument. To get a random number from zero to 10 (inclusive), you can use rand.nextInt(10+1).

But you also have a lower bound. It's correct that you need to add the lower bound to the result as you did, but you need to subtract it from the range first.

You need to change this line:

int randomNum = rand.nextInt (j) + i;

To this:

int randomNum = rand.nextInt(j + 1 - i) + i;

But you need to move this line within your check that i <= j, or else your range becomes negative:

if (i <= j) {
    int randomNum = rand.nextInt(j + 1 - i) + i;
    System.out.format("%4d", randomNum);
} else {
    System.out.format("%4s", "-");
}

Output:

   R    4   5   6   7   8   9  10
   4    4   5   5   7   4   5   8
   5    -   5   6   6   5   5   5
   6    -   -   6   6   8   8   9
   7    -   -   -   7   7   9   9
   8    -   -   -   -   8   9   9
   9    -   -   -   -   -   9   9
  10    -   -   -   -   -   -  10
Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
2

First, you should know how to get random between two integers, and then code the rest (check code comments)

Here is an implementation using ternary operator ?:, which is good to know of

PrintR.java:

import java.util.Random;

public class PrintR {
    public static void main(String[] args) {
        int a = 4;
        int end = 10;
        printRTable(a, end);
    }

    public static void printRTable(int init, int end) {
        Random rand = new Random();
        // first print the top header row
        System.out.format("   R  ");
        for(int i = init; i<=end;i++ ) {
            System.out.format("%4d",i);
        }
        System.out.println();
        System.out.println("------------------------------------------");

        for(int i = init ;i<=end;i++) {
            // print left most column first
            System.out.format("%4d |",i);
            for(int j=init;j<=end;j++) {
                //Ternary operator 
                //r.nextInt(High-Low + 1) + Low; gives you a random number in between Low (inclusive) and High (inclusive)
                System.out.format("%4s", i > j ? "-" : (rand.nextInt(j-i+1)) + i);
            }
            System.out.println();
        }
    }

}

Example output:

   R     4   5   6   7   8   9  10
------------------------------------------
   4 |   4   4   5   7   5   5   4
   5 |   -   5   6   6   5   8  10
   6 |   -   -   6   7   7   6   8
   7 |   -   -   -   7   7   7  10
   8 |   -   -   -   -   8   8  10
   9 |   -   -   -   -   -   9  10
  10 |   -   -   -   -   -   -  10
Rcordoval
  • 1,932
  • 2
  • 19
  • 25