how to generate random numbers in java between a range code example

Example 1: java random numbers in specific range

import java.util.Random;

Random rand = new Random();
int random_integer = rand.nextInt(upperbound-lowerbound) + lowerbound;

Example 2: java generate random integer in range

import java.util.concurrent.ThreadLocalRandom;

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

Example 3: java random usage

import java.util.Random;

Random rndm = new Random();
int mnmbr = 10;

int rndNumber = rndm.nextInt(mnumber) + 1;

System.out.println(rndNumber);

Example 4: java random number generator in range

int rand = ThreadLocalRandom.current().nextInt(x,y);