There is no random number generator between two values ββin Java, just as Python does. To generate Random, this actually takes only one value. Then you need to add ONE SOME NUMBER to the number generated, which will lead to the number being within the range. For example:
package RandGen; import java.util.Random; public class RandGen { public static Random numGen =new Random(); public static int RandNum(){ int rand = Math.abs((100)+numGen.nextInt(100)); return rand; } public static void main(String[]Args){ System.out.println(RandNum()); }
}
This program function lies entirely on line 6 (the first starts with "int rand ...". Note: Math.abs () simply converts the number to an absolute value and is declared as int, which is not very important. The first (100) is the number which I ADD to random. This means that the new exit number will be a random number + 100. numGen.nextInt () is the value of the random number itself, and because I put (100) in its parentheses, it is any number from 1 to 100. Therefore, when I add 100, it becomes a number from 101 to 200. In fact, you are not GENERATING a number from 100 to 200, you add to one from 1 to 100.
David Behrens Oct 11 2018-12-12T00: 00Z
source share