Java.util Bag--random class (Random)

Source: Internet
Author: User

Normal 0 7.8 lbs 0 2 false false en-US zh-cn x-none java.util Packet--Random class (Random)

Author: zccst

The content of the Java.util package focuses on the section:

1. Random Class (Random)

2. StringTokenizer class

3. Collections and Mappings (Bitset, Vector, Stack, hashable)

Random Class (Random) -- Java one of several ways to produce random numbers

Comparison of methods Random () and Java.util.Random in Java.lang.Math:

1, the public static double random () returns a value range of [0.0, 1.0) and is approximately uniformly distributed. On the first call, a new pseudo random number builder is created.

Java.util.Random also creates a new pseudo random number generator. And this method is fully synchronized, allowing multiple threads to use without errors.

2, Java.util.Random can produce random numbers of types such as int, long, float, double, and Goussian. This is the biggest difference between it and the method Random () in Java.lang.Math, which only produces a double random number.

In addition, there is a Currenttimemillis () method in the System class that returns a number of milliseconds from January 1, 1970 0:0 0 to the current one, and the return type is long, we can take him as a random number, we can take him on some number Take the mold, you can limit him within a range.

java.util.Random random number class

1, the construction method of random Class (Random) (2)

Public Random ()

Public Random (Longseed)

Java generates random numbers need to have a base value seed, in the first method the base value defaults, then the system time as seed.

2, Random Class (Random) Common methods (6)

public int Nextint ()//The method is to produce an int-type random number. [0-n)

Public long Nextlong ()//The method is to produce a long random number.

public float nextfloat ()//The method is to produce a float-type random number. [0.0f-1.0f)

Public double nextdouble ()//The method is to produce a double random number. [0.0d-1.0d)

public void nextbytes (byte[] bytes); A byte array that generates random data, stored in byte[]

Public synonronized void Setseed (longseed)//The method is to set the base value seed.

Public synchronized Double Nextgoussian ()//The method is to produce a double type of Goussian random number.

Example: Randomnumber.java

The source code is as follows:

Import Java.util.Random;

public class randomnumber{

public static void Main (string[] args) {

Random rand1=new Random ();

Byte[] b= new byte[5];

Rand1.nextbytes (b);

for (int i=0;i<5;i++)

System.out.print (b[i]+ "/t");

System.out.println ();

for (int i=0; i<5;i++)

System.out.print (Rand1.nextint) + "/t");

System.out.println ();

for (int i=0; i<3;i++)

System.out.print (rand1.nextdouble () + "/t");

System.out.println ();

}

}

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.