Random
The Random class is a generator of
pseudorandom numbers. These are called pseudorandom
numbers because they are simply uniformly distributed sequences. Random defines the following
constructors:
Random(
) Random(long seed)
The
first version creates a number generator that uses a reasonably unique seed.
The second form allows you to specify a seed value manually.
If you
initialize a Random object with a
seed, you define the starting point for the random sequence. If you use the
same seed to initialize another Random
object, you will extract the same random sequence. If you want to generate
different sequences, specify different seed values. One way to do this is to
use the current time to seed a Random
object. This approach reduces the possibility of getting repeated sequences.
The core
public methods defined by Random are
shown in Table 19-7. These are the methods that have been available in Random for several years (many since
Java 1.0) and are widely used.
As you
can see, there are seven types of random numbers that you can extract from a Random object. Random Boolean values
are available from nextBoolean( ).
Random bytes can be obtained by
calling nextBytes( ). Integers can
be extracted via the nextInt( )
method. Long integers, uniformly distributed over their range, can be obtained
with nextLong( ). The nextFloat( ) and nextDouble( ) methods return a uniformly distributed float and double, respectively, between 0.0 and 1.0. Finally, nextGaussian( ) returns a double value centered at 0.0 with a standard deviation of 1.0. This is what is
known as a bell curve.
Here is
an example that demonstrates the sequence produced by nextGaussian( ). It obtains 100 random Gaussian values and averages
these values. The program also counts the
Method : Description
boolean
nextBoolean( ) : Returns the next boolean random number.
void
nextBytes(byte vals[ ]) : Fills vals with randomly generated values.
double
nextDouble( ) : Returns the next double random number.
float
nextFloat( ) : Returns the next float random number.
double
nextGaussian( ) : Returns the next Gaussian random number.
int
nextInt( ) : Returns the next int random number.
int
nextInt(int n) : Returns the next int random number within the range zero to n.
long
nextLong( ) : Returns the next long random number.
void
setSeed(long newSeed) : Sets the seed value (that is, the starting point for
the random number generator) to that specified by newSeed.
Table 19-7 The Core Methods Defined by Random
number
of values that fall within two standard deviations, plus or minus, using
increments of 0.5 for each category. The result is graphically displayed
sideways on the screen.
// Demonstrate random
Gaussian values.
import java.util.Random;
class RandDemo {
public static void
main(String args[]) { Random r = new Random();
double val; double sum = 0;
int bell[] = new int[10];
for(int i=0; i<100; i++) {
val = r.nextGaussian(); sum += val;
double t = -2;
for(int x=0; x<10; x++, t
+= 0.5) if(val < t) {
bell[x]++;
break;
}
}
System.out.println("Average
of values: " + (sum/100));
// display bell curve,
sideways
for(int i=0; i<10; i++) {
for(int x=bell[i]; x>0;
x--) System.out.print("*");
System.out.println();
}
}
}
Here is
a sample program run. As you can see, a bell-like distribution of numbers is
obtained.
Average of values:
0.0702235271133344
**
*******
******
***************
******************
*****************
*************
**********
********
***
JDK 8
adds three new methods to Random
that support the new stream API (see Chapter 29). They are called doubles( ), ints( ), and longs( ),
and each returns a reference to a stream that contains a sequence of
pseudorandom values of the specified type. Each method defines several
overloads. Here are their simplest forms:
DoubleStream
doubles( ) IntStream ints( ) LongStream longs( )
The doubles( ) method returns a stream that
contains pseudorandom double values.
(The range of these values will be less than 1.0 but greater than 0.0.) The ints( ) method returns a stream that
contains pseudorandom int values.
The longs( ) method returns a stream
that contains pseudorandom long
values. For these three methods, the stream returned is effectively infinite.
Several overloads of each method are provided that let you specify the size of
the stream, an origin, and an upper bound.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.