Answers for "random java negative to positive number"

3

How to add negative random numbers in java

//To get a random number between a set range with min and max:

int number = random.nextInt(max - min) + min;
//It also works with negative numbers.

//So:

random.nextInt(30 + 10) - 10;
// max = 30; min = -10;
//Will yield a random int between -10 and 30 (exclusive).

//It also works with doubles.
Posted by: Guest on September-18-2020
4

Make a negative number positive Java

Math.abs(-5); // returns 5
Posted by: Guest on April-09-2021

Code answers related to "random java negative to positive number"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language