Java random string tool class

1、 Generation of java random numbers

In Java, there are three concepts of random numbers in a broad sense. 1. Through system Currenttimemillis() to get a long number of milliseconds of the current time. 2. Pass math Random () returns a double value between 0 and 1. 3. Generate a random number through the random class. This is a professional random tool class with powerful functions.

2、 Random class API description

1. The Java API describes that the instance of random class is used to generate pseudo-random number stream. This class uses 48 bit seeds, Use the linear congruence formula to modify it (see the art of computer programming, Volume 2 by Donald Knuth) , Section 3.2 Section 1). If two random instances are created with the same seed, the same sequence of method calls is made to each instance, and they will generate and return the same sequence of numbers. In order to ensure the implementation of attributes, a specific algorithm is specified for class random. Many applications will find the random method in the math class easier to use.

2. Method summary random() creates a new random number generator.

Random (long seed) creates a new random number generator using a single long seed: public random (long seed) {setseed (seed);} The next method uses it to save the state of the random number generator.

Protected int next (int bits) generates the next pseudo-random number.

Boolean nextboolean() returns the next pseudo-random number, which is a uniformly distributed Boolean value taken from the sequence of this random number generator.

Void nextbytes (byte [] bytes) generates random bytes and places them in the byte array provided by the user.

Double nextdouble() returns the next pseudo-random number, which is a double value taken from the sequence of this random number generator and evenly distributed between 0.0 and 1.0.

Float nextfloat() returns the next pseudo-random number, which is a float value evenly distributed between 0.0 and 1.0 taken from the sequence of this random number generator.

Double nextgaussian() returns the next pseudo-random number, which is a Gaussian ("normal") distributed double value taken from the sequence of this random number generator, with an average of 0.0 and a standard deviation of 1.0.

Int nextint() returns the next pseudo-random number, which is an evenly distributed int value in the sequence of this random number generator.

Int nextint (int n) returns a pseudo-random number, which is an int value taken from the sequence of this random number generator and evenly distributed between 0 (inclusive) and the specified value (exclusive).

Long nextlong() returns the next pseudo-random number, which is a uniformly distributed long value taken from the sequence of this random number generator.

Void setseed (long seed) seed this random number generator with a single long seed.

3、 Random class instructions

1. The difference between seeded and seedless random class is based on the instance of strategy seeded and seedless random. Generally speaking, the difference between the two is: with seeds, the results generated by each run are the same. If there is no seed, the results generated in each run are random and have no rules. 2. Create a random object without seeds. Random random = new random(); 3. There are two methods to create random objects without seeds: 1) random random = new random (555l);    2) Random random = new Random();    random. setSeed(555L);

4、 Comprehensive application

The following shows the usage through a recently written random number tool class:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>