Choose between two numbers in Java

Asked

Viewed 165 times

1

I’m building a game in which I want an object with given coordinates on the ground to spread, creating more pixels of himself on the ground. Each pixel of which would be represented by number 4.

Here comes the code:

public void interact(mainclass a) {
    a.terrain[x][y] = 4;
    Random rand = new Random();
    int directionX = escolher(-1, 1);
    int directionY = escolher(-1, 1);
    a.terrain[x + directionX][y + directionY] = 4;
}

In class mainclass, I have the information of the land, as well as the land itself, in the form of int[][].

The direction in which the given object spreads is random. The problem is that, for the function to choose, I tried to use the Random rand to generate a random number between -1 and 1, the problem is that there is still 0, between -1 and 1.

The function escolher does not exist, just wrote to give an example.

I mean, I can’t use the Random. I have to find a way to choose literally between both numbers (-1 and 1).

How to do this?

1 answer

1


Of course you can use Random. If there are only two possible values to choose from, one option would be:

public static Random rand = new Random();

public int escolher() {
    int n = rand.nextInt(2);
    return n == 1 ? n : -1;
}

...
int directionX = escolher();

rand.nextInt(2) returns a random number (actually pseudo-random) between zero (inclusive) and 2 (exclusive). That is, it only returns zero or one.

So if the number is 1, I return it myself. Otherwise, I return -1.

Also, you don’t need to create a Random each time the method is called, you can create one only once, outside the method.


If you want the method to receive all possible values, just modify it a little:

public int escolher(int... valores) {
    int n = rand.nextInt(valores.length);
    return valores[n];
}

// escolhe -1 ou 1
int directionX = escolher(-1, 1);

// escolhe 1, 2, 5 ou 90
int directionX = escolher(1, 2, 5, 90);

I used the syntax of varargs, so you can pass as many values as needed.


The only however is that if you pass no number (ie just call escolher()) will give error, because in this case valores.length will be zero and nextInt should receive a number greater than zero (otherwise he throws exception).

Passing just one number is also kind of useless (escolher(x) will always return the x, regardless of the value that x have), then you can include a validation and launch an exception if the method receives less than two values:

public int escolher(int... valores) {
    if (valores.length < 2)
        throw new IllegalArgumentException("Você deve passar pelo menos 2 valores");
    int n = rand.nextInt(valores.length);
    return valores[n];
}

// escolhe -1 ou 1
int directionX = escolher(-1, 1);

// as duas chamadas abaixo lançam exceção
int directionX = escolher(-1);
int directionX = escolher();
  • Thank you. Just what I needed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.