Jdk_ instance (Java How to generate random numbers)

Source: Internet
Author: User

The following are from:

http://scliu123.iteye.com/blog/506387

--------------------------
Package book.number;

Import Java.util.Random;
/**
* The class Java.util.Random in the Java Utility Class Library provides a way to generate random numbers of various types.
* It can produce random numbers of types such as int, long, float, double, and Goussian.
* The method random () in Java.lang.Math produces only random numbers of double types.
*/
public class Randomnumber {

public static void Main (string[] args) {

Generating random numbers using the Java.lang.Math random method
System.out.println ("Math.random ():" + math.random ());

Constructing a Java.util.Random object using a construction method without parameters
System.out.println ("Random object constructed using a constructor without parameters:");
Random Rd1 = new Random ();
Generate random numbers of various types
Generate integers by uniform distribution
SYSTEM.OUT.PRINTLN ("int:" + rd1.nextint ());
produce long integers by uniform distribution
System.out.println ("Long:" + Rd1.nextlong ());
Generate a float that is greater than or equal to 0, or less than 1, by uniform distribution [0, 1]
System.out.println ("float:" + rd1.nextfloat ());
Double number of [0, 1] range by uniform distribution
System.out.println ("Double:" + rd1.nextdouble ());
Generate random numbers by normal distribution
System.out.println ("Gaussian:" + Rd1.nextgaussian ());

Generate a series of random numbers
System.out.print ("Random Integer Sequence:");
for (int i = 0; i < 5; i++) {
System.out.print (Rd1.nextint () + "");
}
System.out.println ();

Specify the range that the random number produces
System.out.print ("[0,10) range of random integer Sequences:");
for (int i = 0; i < i++) {
The random nextint (int n) method returns a random number within the range of [0, N]
System.out.print (Rd1.nextint (10) + "");
}
System.out.println ();
System.out.print ("[5,23) range of random integer Sequences:");
for (int i = 0; i < i++) {
Because the range of nextint (int n) methods starts at 0,
So we need to convert the interval [5,28] to 5 + [0, 23].
System.out.print (5 + rd1.nextint (23) + "");
}
System.out.println ();
System.out.print ("Using nextfloat () to generate a sequence of random integers in the [0,99) Range:");
for (int i = 0; i < i++) {
System.out.print ((int) (Rd1.nextfloat () * 100) + "");
}
System.out.println ();
System.out.println ();

Constructing random objects using a constructor with parameters
The constructor parameter is a long type and is the seed that generates random numbers.
System.out.println ("Random object constructed using a constructed method with parameters:");
Random ran2 = new Random (10);
For random objects with the same seed, the sequence of random numbers generated is the same.
SYSTEM.OUT.PRINTLN ("Generate [0,10) of random integer sequences using seed-10 random objects:");
for (int i = 0; i < i++) {
System.out.print (Ran2.nextint (10) + "");
}
System.out.println ();
Random ran3 = new Random (10);
SYSTEM.OUT.PRINTLN ("Generates a sequence of random integers in [0,10) using a random object with another seed of 10:");
for (int i = 0; i < i++) {
System.out.print (Ran3.nextint (10) + "");
}
System.out.println ();
The sequence of random numbers generated by ran2 and Ran3 is the same, if you use two random objects that are not generated with a parameter constructor,
This is not the case because the seed default of the random object that is generated without the parameter constructor is the number of milliseconds of the current system time.

In addition, using random directly cannot avoid generating duplicate numbers, and if you need to generate a sequence of random numbers with no duplicates, you need to use arrays and collection classes
The 4th chapter of this book will give a solution.
}
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.