Web Development and Design | Tutorial for Java, PHP, HTML, Javascript: how-to-generate-random-numbers-in-java-within-range

Web Development and Design | Tutorial for Java, PHP, HTML, Javascript: how-to-generate-random-numbers-in-java-within-range
Showing posts with label how-to-generate-random-numbers-in-java-within-range. Show all posts
Showing posts with label how-to-generate-random-numbers-in-java-within-range. Show all posts

Java Generating Better Random Numbers

Java Generating Better Random Numbers

Problem

You need to generate better random numbers.

Solution

Construct a java.util.Random object (not just any old random object) and call its
next*( ) methods. These methods include nextBoolean( ) , nextBytes( ) (which fills
the given array of bytes with random values), nextDouble( ) , nextFloat( ) , nextInt( ) ,
and nextLong( ) . Don’t be confused by the capitalization of Float , Double , etc. They
return the primitive types boolean , float , double , etc., not the capitalized wrapper
objects. Clear enough? Maybe an example will help:

// Random2.java
// java.util.Random methods are non-static, so need to construct
Random r = new Random( );
for (int i=0; i<10; i++)
System.out.println("A double from java.util.Random is " + r.nextDouble( ));
for (int i=0; i<10; i++)
System.out.println("An integer from java.util.Random is " + r.nextInt( ));

You can also use the java.util.Random nextGaussian( ) method, as shown next. The nextDouble( ) methods try to give a “flat” distribution between 0 and 1.0, in which each value has an equal chance of being selected. A Gaussian or normal distribution is a bell-curve of values from negative infinity to positive infinity, with the majority of the values around zero (0.0).

// Random3.java
Random r = new Random( );
for (int i=0; i<10; i++)
System.out.println("A gaussian random double is " + r.nextGaussian( ));

See Also

The Javadoc documentation for java.util.Random , and the warning in the Introduc- tion about pseudo-randomness versus real randomness. For cryptographic use, see class java.security.SecureRandom , which provides crypto- graphically strong pseudo-random number generators (PRNG).

Java Generating Random Numbers

Java Generating Random Numbers


Problem

You need to generate random numbers in a hurry.

Solution

Use java.lang.Math.random( ) to generate random numbers. There is no claim that
the random values it returns are very good random numbers, however. This code
exercises the random( ) method:

// Random1.java
// java.lang.Math.random( ) is static, don't need to construct Math
System.out.println("A random from java.lang.Math is " + Math.random( ));

Note that this method only generates double values. If you need integers, you need to scale and round:

/** Generate random ints by asking Random( ) for
* a series of random integers from 1 to 10, inclusive.
*
* @author Ian Darwin, http://www.darwinsys.com/
* @version $Id: ch05,v 1.5 2004/05/04 20:11:35 ian Exp $
*/
public class RandomInt {
public static void main(String[] a) {
Random r = new Random( );
for (int i=0; i<1000; i++)
// nextInt(10) goes from 0-9; add 1 for 1-10;
System.out.println(1+Math.round(r.nextInt(10)));
}
}

To see if it was really working well, I used the Unix tools sort and uniq, which, together, give a count of how many times each value was chosen. For 1,000 integers, each of 10 values should be chosen about 100 times:

C:> java RandomInt | sort -n | uniq -c
110 1
106 2
98 3
109 4
108 5
99 6
94 7
91 8
94 9
91 10
C:>