Java Random contains the difference between participating in a non-parametric constructor

Source: Internet
Author: User

# #Random is commonly used as a random number generator, it has two construction methods:

        Random random = new Random();        Random random2 = new Random(50);

1. No parameter construction method:

public Random() {    setSeed(System.nanoTime() + seedBase);    ++seedBase;}

2. Construction method with parameters:

public Random(long seed) {    setSeed(seed);}

The Setseed method is called:

public synchronized void setSeed(long seed) {    this.seed = (seed ^ multiplier) & ((1L << 48) - 1);    haveNextNextGaussian = false;}

# # #可以看到, the non-parametric construction method uses the current time as a seed each time, and the method with the parameter construction is a fixed value as the seed

# #什么是种子 seed?

Seed is the parameter used when random numbers are generated:

The most important thing in Random is the next (int) method, which is calculated using seed:

protected synchronized int next(int bits) {    seed = (seed * multiplier + 0xbL) & ((1L << 48) - 1);    return (int) (seed >>> (48 - bits));}

The other nextxxx methods are called Next ().

such as Nextint (int):

public int nextInt(int n) {    if (n <= 0) {        throw new IllegalArgumentException("n <= 0: " + n);    }    if ((n & -n) == n) {//调用 next()        return (int) ((n * (long) next(31)) >> 31);    }    int bits, val;    do {        bits = next(31);        val = bits % n;    } while (bits - val + (n - 1) < 0);    return val;}

Again such as Nextboolean ():

//也是调用的 next()public boolean nextBoolean() {    return next(1) != 0;}

# #举个栗子:

@Testpublic void testRandomParameter(){    System.out.println("Random 不含参构造方法:");    for (int i = 0; i < 5; i++) {        Random random = new Random();        for (int j = 0; j < 8; j++) {            System.out.print(" " + random.nextInt(100) + ", ");        }        System.out.println("");    }    System.out.println("");    System.out.println("Random 含参构造方法:");    for (int i = 0; i < 5; i++) {        Random random = new Random(50);        for (int j = 0; j < 8; j++) {            System.out.print(" " + random.nextInt(100) + ", ");        }        System.out.println("");    }}

The 5 random generator objects are created by using the method of constructing with the parameter and without the parameter, and each random generator reproduces 8 random numbers, and compares the results:

Run once more:

# #总结:

From the above examples, we can find:

A random number is a seed that is generated by calculation.

    • Constructors with no parameters use the current time as a seed each time, more randomness
    • The constructors with parameters are pseudo-random and more predictable.

Java Random contains the difference between participating in a non-parametric constructor

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.