220

I want to generate a number between 1 and 10 in Java.

Here is what I tried:

Random rn = new Random();
int answer = rn.nextInt(10) + 1;

Is there a way to tell what to put in the parenthesis () when calling the nextInt method and what to add?

6
  • 7
    Did you read the documentation, which explains exactly how to use this function?
    – SLaks
    Dec 5, 2013 at 1:52
  • 13
    @SLaks I actually did, I was still confused after.
    – Shania
    Dec 5, 2013 at 3:55
  • You can just put it in your code: int randomNumber = ThreadLocalRandom.current().nextInt(1, 10 + 1); May 21, 2017 at 19:35
  • If you wanna see the code check here - grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/… Sep 27, 2017 at 12:41
  • System.out.println( (int)(Math.random() * (max-min+1) + min)) I use this to generate a random number between min and max inclusively
    – HibaHasan
    Jan 20, 2022 at 8:14

3 Answers 3

308

As the documentation says, this method call returns "a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)". This means that you will get numbers from 0 to 9 in your case. So you've done everything correctly by adding one to that number.

Generally speaking, if you need to generate numbers from min to max (including both), you write

random.nextInt(max - min + 1) + min
4
97

The standard way to do this is as follows:

Provide:

  • min Minimum value
  • max Maximum value

and get in return a Integer between min and max, inclusive.

Random rand = new Random();

// nextInt as provided by Random is exclusive of the top value so you need to add 1 

int randomNum = rand.nextInt((max - min) + 1) + min;

See the relevant JavaDoc.

As explained by Aurund, Random objects created within a short time of each other will tend to produce similar output, so it would be a good idea to keep the created Random object as a field, rather than in a method.

1
  • 7
    Random rand = new Random(); I would go so far as to say that it must be a field. Random objects created within a short time of each other will tend to produce similar output. So many calls to randInt within a short period of time will not give evenly distributed output.
    – Aurand
    Dec 5, 2013 at 1:56
25

This will work for generating a number 1 - 10. Make sure you import Random at the top of your code.

import java.util.Random;

If you want to test it out try something like this.

Random rn = new Random();

for(int i =0; i < 100; i++)
{
    int answer = rn.nextInt(10) + 1;
    System.out.println(answer);
}

Also if you change the number in parenthesis it will create a random number from 0 to that number -1 (unless you add one of course like you have then it will be from 1 to the number you've entered).

3
  • why is this i < 100 inside parenthesis of for? It should be 10 Sep 4, 2015 at 10:01
  • 2
    It will generate 100 numbers between 1-10 Sep 4, 2015 at 10:02
  • This answer mostly repeats the previous answers without providing a new alternative. Oct 13, 2015 at 12:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.