Generating Random Numbers within a Range
In some applications, the programmer may be required to generate random numbers within a range as in lottery tickets. The range may be zero to some number or between any two numbers.
import java.util.Random;
public class RangeNumbers2
{
public static void main(String args[])
{
System.out.println("\t\tNumbers 0 to 200");
Random r1 = new Random();
for(int i = 0; i < 5; i++)
{
int k = r1.nextInt(200);
System.out.print(k + "\t");
}
System.out.println("\n\t\tNumbers within 10 to 30");
int startRange = 10, endRange = 30;
for(int i = 0; i < 5; i++)
{
int offsetValue = endRange - startRange + 1;
int baseValue = (int) (offsetValue * r1.nextDouble());
int k = baseValue + startRange;
System.out.print(k + "\t");
}
}
}
Random r1 = new Random();
Random r2 = new Random(150);
The Random class constructor is overloaded – one that takes no parameter and the other that takes an integer value (known as seed ). The first object r1 generates different random numbers at different times of execution. The second object r2 with a seed of 150 generates same set of random numbers at different times of execution. Here, the seed value is 150. Different seed values generate different sets. The random numbers with a seed value is useful to test and bug-fix the software; when the same values are fed, the output of the application should be the same. Random numbers are not thread-safe. Programmer should take care of with concurrency problems in a multithreaded environment.
The nextInt() method returns an integer value and similar methods exist, with Random class, like nextDouble(), nextFloat() and nextLong() etc. that returns a double, float and long values.
The nextInt(200) returns an integer value from 0 to 199. To print between any two values, logic included is self explanatory.
Applications of Random Numbers
The importance of random numbers is increasing day-by-day. Some applications are given below.
- Applications of games as in dice-ware
- Applications of testing software for searching or sorting
- Applications of testing with different input values
- Applications of encryption to give guarantee for secrecy
The following program uses random() method of Math class to generate random numbers.
public class UsingMathRandom
{
public static void main(String args[])
{
for(int i = 0; i < 5; i++)
{
double k = Math.random();
System.out.println(k);
}
}
}
The random() method of Math class produces a double values between 0 to 1.0 (not inclusive both).
sir can we do any restriction on the random number generations i.e only 2 digit integer number generation like that
You must write extra code. The following code prints from 10 to 99.
public class Demo
{
public static void main(String[] args)
{
int x = 10 + (int) (Math.random() * 90);
System.out.println(x);
}
}
sir here we are writing class name is different and object creation is with different class name what is this meaning and what is this nextInt() method what is this method??
Your class name is that one on which you open file to write the code. In your code you can create an object of any class. This is called “composition”. See way2java for composition.