class Random Generate random numbers
Summary: By the time you complete this "class Random Generate random numbers", you will understand of how to generate random numbers in Java.
Introduction to class Random Generate random numbers
Generating different numbers at different times of execution, by the same program, is known as random number generation. Random numbers are useful in software product checking and in developing games (as in Dice). Different techniques and algorithms exist, in the software world, to generate random numbers.
There are two ways to generate random numbers by a Java programmer; one is by using the method java.lang.Math.random() and the other is using the methods of class java.util.Random. Usage of Random class is more flexible. The random() method of Math class generates random numbers of double values within the range of 0 and 1 (both not inclusive). These double values can be of positive or negative. But with Random class, we can produce ints, longs and doubles etc. with seed or without seed. When seed is supplied, we get the same sequence of random numbers every time the program is executed. With a little bit extra code, random numbers within a range can also be produced.
Following is the class signature
public class Random extends Object implements Serializable
Generating Random Numbers
In random number generation, the same code executed at different times, produces different results. Following program illustrates the technique of producing random numbers with and without a seed.
import java.util.Random;
public class RandomNumbers1
{
public static void main(String args[])
{
Random r1 = new Random(); // without seed
System.out.println("\t\tGenerating Random numbers without seed value");
for(int i = 0; i < 5; i++)
{
int k = r1.nextInt();
System.out.print(k + "\t");
}
// with a seed of 150
Random r2 = new Random(150);
System.out.println("\n\t\tGenerating Random numbers with a seed value of 150");
for(int i = 0; i < 5; i++)
{
int k = r2.nextInt();
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. Different seed values generate different sets. The random numbers with a seed value are useful to test and bug-fix the software; when the same values are fed, the output of the application should come the same. Random numbers are not thread-safe. Programmer should take care of with concurrency problems in a multithreaded environment.
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.