Friday, August 14, 2009

Java : Generate Random Numbers between minimum and maximum range.

This is simple java program to generate random numbers between minimum and maximum (both inclusive) given range. For example in below code input minimum number is 200 and maximum number is 300.

/* RandomNumber.java*/

import java.util.Random;

public class RandomNumber
{
public static void main(String[] args)
{
int min = 200;
int max = 300;

Random ran = new Random();

for (int i=0;i<10 ;i++ )
{
System.out.println(ran.nextInt(max - min + 1) + min);
}
}
}



/*****OUTPUT*****/

272
251
214
245
246
228
240
212
294
285

No comments:

Post a Comment