mercredi 2 janvier 2019

Trouble generating random -1 through +1 in Java

I'm trying to figure out how to best generate a random number between -1 and 1. I thought this was the formula below but my output is always 0s and 1s, no negatives.

random.nextInt(max + 1 - min) + min

Here's the actual code:

   int xRand = random.nextInt((1 + 1 + 1) - 1);
   int yRand = random.nextInt((1 + 1 + 1) - 1);

    System.out.println("x: " + xRand);
    System.out.println("y: " + yRand);




Aucun commentaire:

Enregistrer un commentaire