1

Recently I've started learning Java and been having plenty of problems. I have tried searching on the website, but I can't seem to get the answer I need. Apologies if there is an answer and the mistake is on my part.

Please take a look at my code first:

import java.util.Random;


public class Test {
    public static void main (String args[]) {
        Random num = new Random();
        int[][] multi = new int [3][];
        int test = 1;             // comments for stackoverflow questions   
        int test2 = 10;           // how can i create something that will range from 1-9?
        int test3 = test2-test;   // trying this has always generated 0-9 instead

    multi[0] = new int [] {num.nextInt(test3), num.nextInt(test3), num.nextInt(test3)};
    multi[1] = new int [] {num.nextInt(test3), num.nextInt(10), num.nextInt(10)};
    multi[2] = new int [] {test3, test3, num.nextInt(10)};

// PLEASE IGNORE THE 2D ARRAYS, it's just something I am testing on

    int rows = 3;
    int columns = 3;

    for(int i=0; i<rows; i++) {
        for (int j=0; j<columns; j++) {
            System.out.print(multi[i][j] + "\t");
        }
        System.out.println();
    }
    }   
}

I am trying to simulate a jackpot, whereby I want to generate numbers between 1 to 9 in all of the 2D array I have created. However I do not know how to generate the numbers between 1 to 9, I have creating only ranges 0-10 and 0-9

My idea is that, once I complete this 2D array of ranges 1-9, then I could use a certain amount of if-else statements to make the user win the jackpot whenever the center row or diagonal rows has 3 of the same numbers.

I have also tried using:

int test3 = num.nextInt(test2-test+1)-test;

This would generate numbers between 1-9 but if I were to use it all, it will always generate the same numbers to me, otherwise it gives an exception because of negative numbers.

3
  • 1
    Consider adding "1" to your random result to change the "lower" boundary. Commented Apr 10, 2015 at 16:57
  • @Jägermeister what do you mean? Commented Apr 10, 2015 at 17:04
  • @reyareya I think he means generate a random number between 0-8 and then add 1 to that result. Commented Apr 10, 2015 at 17:06

3 Answers 3

1

if you only want a random number between inclusive 1 and 9, this should help:

Random r = new Random();
int num = r.nextInt(9) + 1;
Sign up to request clarification or add additional context in comments.

Comments

0

You can generate random number like this:

Math.rand() * i // Generate number between 0 and i-1

Or

  Math.rand() * i + 1 // Generate number between 1 and i

1 Comment

He is already generating the number it sounds, but it is 0-9, when he wants 1-9.
0

To generate a value from 1-9:

Random random = new Random();
int n = random.nextInt(9) + 1;

To apply this in your case:

multi[0] = new int [] 
   {num.nextInt(9) + 1, num.nextInt(9) + 1, num.nextInt(9) + 1};

2 Comments

ohmygod!!! thanks! as simple as this looks/sounds, this works perfectly. I never thought that using a simple "+1" would work immediately. I thought it was a problem of the integers i was using.....
Maybe you just thought the Random.nextInt() method always returns a value of at least 1? ;) I suggest you read the Java API, it's quite good for learning exactly what you can do with its standard libraries. docs.oracle.com/javase/7/docs/api/java/util/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.