# Java生成两个给定值之间的随机数

``Random r = new Random(); for(int i = 0; i < a.length; i++){ for(int j = 0; j < a[i].length; j++){ a[i][j] = r.nextInt(); } }` `

` `Random r = new Random(); int Low = 10; int High = 100; int Result = r.nextInt(High-Low) + Low;` `

` `int r = (int) (Math.random() * (upper - lower)) + lower;` `

` `int Random = (int)(Math.random()*100);` `

` ` for (int i = 1; i <= 10 ; i++) { int Random = (int)(Math.random()*100); System.out.println(Random); }` `

` ` int Random =10 + (int)(Math.random()*(91)); /* int Random = (min.value ) + (int)(Math.random()* ( Max - Min + 1)); *Where min is the smallest value You want to be the smallest number possible to generate and Max is the biggest possible number to generate*/` `

` `Random random = new Random(); int randomNumber = random.nextInt(upperBound - lowerBound) + lowerBound;` `

` `a[i][j] = r.nextInt(101);` `

Java没有像Python那样在两个值之间有一个Random生成器。 它实际上只需要一个值来生成随机数。 那么你需要做的是在生成的数字中join一个一定的数字，这将导致数字在一个范围内。 例如：

` `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()); }` `

}

` `public class RandomInt { public static void main(String[] args) { int n1 = Integer.parseInt(args[0]); int n2 = Integer.parseInt(args[1]); double Random; if (n1 != n2) { if (n1 > n2) { Random = n2 + (Math.random() * (n1 - n2)); System.out.println("Your random number is: " + Random); } else { Random = n1 + (Math.random() * (n2 - n1)); System.out.println("Your random number is: " +Random); } } else { System.out.println("Please provide valid Range " +n1+ " " +n2+ " are equal numbers." ); } } }` `