## Generating a Random Number between 1 and 10 Java?

`max`
`min`
`random.nextInt(max - min + 1) + min`

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.

Note

## Generating a Random Number between 1 and 10 Java?

```/**
* Returns a psuedo-random number between min and max, inclusive.
* The difference between min and max can be at most
* <code>Integer.MAX_VALUE - 1</code>.
*
* @param min Minimim value
* @param max Maximim value.  Must be greater than min.
* @return Integer between min and max, inclusive.
* @see java.util.Random#nextInt(int)
*/
public static int randInt(int min, int max) {

// Usually this can be a field rather than a method variable
Random rand = new Random();

// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = rand.nextInt((max - min) + 1) + min;

return randomNum;
}```

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 the method as I have done (for explanation purposes only).

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.

The standard way to do this is as follows:

Note

## Generating a Random Number between 1 and 10 Java?

`max`
`min`
`random.nextInt(max - min + 1) + min`

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.

Note

## Generating a Random Number between 1 and 10 Java?

```/**
* Returns a psuedo-random number between min and max, inclusive.
* The difference between min and max can be at most
* <code>Integer.MAX_VALUE - 1</code>.
*
* @param min Minimim value
* @param max Maximim value.  Must be greater than min.
* @return Integer between min and max, inclusive.
* @see java.util.Random#nextInt(int)
*/
public static int randInt(int min, int max) {

// Usually this can be a field rather than a method variable
Random rand = new Random();

// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = rand.nextInt((max - min) + 1) + min;

return randomNum;
}```

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 the method as I have done (for explanation purposes only).

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.

The standard way to do this is as follows:

Note

## Generating a Random Number between 1 and 10 Java?

```Random rn = new Random();

for(int i =0; i < 100; i++)
{
int answer = rn.nextInt(10) + 1;
}```
`import java.util.Random;`
```Random rn = new Random();

for(int i =0; i < 100; i++)
{
int answer = rn.nextInt(10) + 1;
`import java.util.Random;`