Java generates a random number between two given values

I would like to know how to create a random number between two given values.

I can generate a random number with the following:

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

However, how can I create a random number from 0 to 100 (inclusive)?

Thanks to everyone.

+81
java random numbers
Mar 11 '11 at 10:15
source share
7 answers

You can use for example. r.nextInt(101)

For a more general β€œbetween two numbers” use:

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

This gives you a random number between 10 (inclusive) and 100 (exclusive)

+263
Mar 11 '11 at 10:17
source share

Assuming that the upper bound is the upper bound and the lower is the lower bound, you can make a random number r between two bounds:

 int r = (int) (Math.random() * (upper - lower)) + lower; 
+30
Jun 22
source share

Er ...

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

if you need to generate more than one value then just use for loop to do this

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

If you want to specify a more decent range, for example, from 10 to 100 (both are in the range)

so the code will look like this:

  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*/ 
+7
Oct 22 '11 at 20:55
source share

Like this,

 Random random = new Random(); int randomNumber = random.nextInt(upperBound - lowerBound) + lowerBound; 
+3
Jan 18 '14 at
source share

Use Random.nextInt (int) .

In your case, it will look something like this:

 a[i][j] = r.nextInt(101); 
+2
Mar 11 '11 at 10:20
source share

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.

+1
Oct 11
source share

You can also try the following:

 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." ); } } } 
0
Oct. 30 '12 at 18:23
source share



All Articles