-2

how would I create up to 15-20 random numbers between 100-200 in java?

I have this atm but it creates any random numbers but I want the numbers to be between 100 and 200 but I don't know how I would go about adding this to the code below. please can someone help.

Random rand = new Random();
    int Randnum;
    for(int i = 0; i <=20; i++) {
        System.out.println(Randnum + " ");

        }
    }
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170

4 Answers4

0

This has been answered before, but use rand.nextInt(int n). This will generate a number between 0 (inclusive) and n (exclusive). In your case, use rand.nextInt(101)+100 to generate a number between (and including) 100 and 200.

Random rand = new Random();
    int Randnum;
    for(int i = 0; i <=20; i++) {
        Randnum = rand.nextInt(101)+100;
        System.out.println(Randnum + " ");

        }
    }
  • 1
    Yes, it is already covered in the second answer of the possible duplicate Q/A (with a deeper explanation and a generic formula for these cases). When you encounter this, please flag the question as duplicate instead of generating a new answer. – Luiggi Mendoza Nov 20 '13 at 23:13
0
 Random rand = new Random();
    int Randnum;
    for (int i = 0; i <= 20; i++) {
        Randnum =rand.nextInt(101) + 100;
        System.out.println(Randnum + " ");
    }

nextInt(n) method of Random class returns a number between 0(inclusive) and n(exclusive). In your case, you need a number between 100 and 200, so fetch a number using nextInt with values ranging from 0 to 101 (you get numbers from 0 to 100) and add 100 to it to get numbers from 100 to 200.

Adarsh
  • 3,613
  • 2
  • 21
  • 37
0

You can either use Random or Math#random

Алексей
  • 1,847
  • 12
  • 15
0

use Math.random()

You can do something like:

int[] randnum = new int[20];

for(int i = 0; i <20; i++) 
{
      randnum[i] = (int)((Math.random() * 101)+100) ; 

}

now you have 20 integers between 100 and 200.

user3015246
  • 139
  • 1
  • 2
  • 9