Introduction to Random () function usage in Java

Source: Internet
Author: User

Random ()

Creates a new random number generator.

The code is as follows Copy Code
Random (Long Seed)

Creates a new random number generator with a single long seed.

We can specify the seed when constructing the random object, such as:

The code is as follows Copy Code

Random r1 = new random (20);

or the default current system time for the number of times the http://www.111cn.net/jsp/Java/45403.htm should be relative to the number of seeds:

The code is as follows Copy Code

Random r1 = new Random ();


The seed number is only the origin number of the stochastic algorithm, independent of the interval of the generated random numbers.

2. Common methods in random class

Here's a basic introduction to these methods:

The code is as follows Copy Code

A, public boolean nextboolean ()

The function of this method is to generate a random Boolean value that generates a value of true and false that is equal to the probability of 50%.

The code is as follows Copy Code

B, public double nextdouble ()

The function of this method is to generate a random double value between [0,1.0].

The code is as follows Copy Code
c, public int nextint ()

The function of this method is to generate a random int value that is between the range of int, which is 231 to 231-1.

If you need to generate an int value for a specified interval, you need to make some mathematical transformations, as shown in the following example of using code.

The code is as follows Copy Code
d, public int nextint (int n

)

The function of this method is to generate a random int value that is in the interval of [0,n], which is a random int value between 0 and N, containing 0 without n.

If you want to generate an int value for a specified interval, you also need to make some mathematical transformations, see the code in the example below.

The code is as follows Copy Code

E, public void setseed (long Seed)

The function of this method is to reset the number of seeds in the random object. The random object and the same seed number after the seed number is set are created using the New keyword

The same as the random object.

3. Example of using the random class

Using the random class, which is usually generated by randomly generating a specified interval, describes how to generate random numbers for the corresponding interval. The following generates a random number of

The code is generated using the following random object R:

The code is as follows Copy Code

Random r = new Random ();

A, the decimal number of the generation [0,1.0] Interval

The code is as follows Copy Code

Double D1 = r.nextdouble ();

obtained directly using the Nextdouble method.

b, decimal for generating [0,5.0] Interval

The code is as follows Copy Code

Double D2 = r.nextdouble () * 5;

Because the number interval generated by the Nextdouble method is [0,1.0], expanding the interval by 5 times times is the required interval.


Similarly, generating random decimals for the [0,d] interval, and d for any positive decimal, you only need to multiply the return value of the Nextdouble method by D.

c, the decimal number of the generation [1,2.5] Interval

The code is as follows Copy Code

Double d3 = r.nextdouble () * 1.5 + 1;

To generate random decimals for the [1,2.5] interval, you only need to generate a random number for the [0,1.5] interval first, and then add 1 to the generated random number interval.

Similarly, generating random numbers (where D1 is not equal to 0) for any range of fractional ranges [D1,D2] that are not starting at 0, you only need to generate the [0,D2-D1] interval first.

Random numbers, then add D1 to the generated random number range.

d, generate any integer

The code is as follows Copy Code

int n1 = R.nextint ();

You can use the Nextint method directly.

E, the integer that generates the [0,10] Interval

The code is as follows Copy Code

int n2 = R.nextint (10);

N2 = Math.Abs (r.nextint ()% 10);

The above two lines of code can generate an integer for the [0,10] Interval


Uniformity of distribution

The code is as follows Copy Code

Import Java.util.Random;

public class Randomdemo {
/**
* Boy Tribe
*
* @param args
*/
public static void Main (string[] args) {
Random r = new Random ();
int n5 = r.nextint (100);
String ran = "";
if (N5 < 55) {//55-digit interval, 55% chance
ran = "55%";
} else if (N5 < 95) {//[55,95), 40-digit interval, 40% chance
ran = "40%";
} else {
ran = "5%"; Residual odds, you know!
}
System.out.println (RAN);
}
}

Introduction to Random () function usage in Java

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.