Java Generate Random Number Between Two Given Values
Like this,
Random random = new Random();
int randomNumber = random.nextInt(upperBound - lowerBound) + lowerBound;
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*/
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)
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;