222

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

I am able to 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 generate a random number between 0 and 100 (inclusive)?

Mus
  • 7,290
  • 24
  • 86
  • 130

7 Answers7

541

You could use e.g. r.nextInt(101)

For a more generic "in 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 in between 10 (inclusive) and 100 (exclusive)

Ivonet
  • 2,492
  • 2
  • 15
  • 28
Erik
  • 88,732
  • 13
  • 198
  • 189
  • 1
    Thank you - I have tried that but it does not seem to work. I am using the random numbers from 0 to 100 (inclusive) to populate a multidimensional array; when trying this it populates the array with extremely large and extremely small numbers. For example, -3.76556749E8 3.0207573E8 2.033182079E9 -6.86227134E8. – Mus Mar 11 '11 at 10:19
  • Show the rest of the code. What's e.g. `a`. – Erik Mar 11 '11 at 10:20
  • 1
    Could you post how you invoke `r.nextInt()` and with which values? – Thomas Mar 11 '11 at 10:23
  • @Thomas `Random r = new Random(); System.out.println(r.nextInt(High - Low) + Low);` – SnakeDoc Jul 08 '14 at 19:29
  • 1
    With respect to the Java naming convention the variable names should start with lower case character – Jens May 19 '17 at 08:13
  • in this Result range is 10 to 100 ,r.nextInt(High-Low) given range is 0 to 90 – Manjitha Teshara Aug 04 '18 at 06:01
  • Does not work try 1 and 6 - for roll of dice. You guys are all giving wrong answers : `r.nextInt(high-low+1) + low;` – JGFMK Oct 16 '20 at 13:27
  • As described in the answer, the lower value is inclusive and the higher value is exclusive. So you'd need to use 1 and 7 for a dice roll. – Erik Oct 16 '20 at 17:18
67

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

int r = (int) (Math.random() * (upper - lower)) + lower;
Community
  • 1
  • 1
user2512642
  • 687
  • 5
  • 2
18
int Random = (int)(Math.random()*100);

if You need to generate more than one value, then just use for loop for that

 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, like from 10 to 100 ( both are in the range )

so the code would be :

   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*/
Mus
  • 7,290
  • 24
  • 86
  • 130
Vytautas
  • 181
  • 1
  • 2
  • 4
    Maybe a little late, and it's always bound to opinions and code styles, but usually you don't create a variable with an uppercase, so it's "better" to name it "random" instead of "Random". Classes are usually with uppercase :) – Stefan Schouten Sep 29 '14 at 12:20
  • This will fail in case where max is Integer.MAX_VALUE and min is 0. Use Math.round instead of casting to int to solve that. – Krease Apr 09 '15 at 18:06
  • With respect to the Java naming convention the variable names should start with lower case character – Jens May 19 '17 at 08:14
8

Like this,

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

Java doesn't have a Random generator between two values in the same way that Python does. It actually only takes one value in to generate the Random. What you need to do, then, is add ONE CERTAIN NUMBER to the number generated, which will cause the number to be within a range. For instance:

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's function lies entirely in line 6 (The one beginning with "int rand...". Note that Math.abs() simply converts the number to absolute value, and it's declared as an int, that's not really important. The first (100) is the number I am ADDING to the random one. This means that the new output number will be the 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 between 1 and 100. So when I add 100, it becomes a number between 101 and 200. You aren't actually GENERATING a number between 100 and 200, you are adding to the one between 1 and 100.

Sergey K.
  • 24,894
  • 13
  • 106
  • 174
2

Use Random.nextInt(int).

In your case it would look something like this:

a[i][j] = r.nextInt(101);
MAK
  • 26,140
  • 11
  • 55
  • 86
1

One can also try below:

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." );
        }                     
    }
}
Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
Sunil
  • 19
  • 1
  • 3
    Bit verbose and clumsy. You shouldn't need to repeat that code, just to decide whether to have n1 or n2's value first. – itsbruce Oct 30 '12 at 18:44
  • 1
    With respect to the Java naming convention the variable names should start with lower case character – Jens May 19 '17 at 08:14