Java_ Common Class 09_random class

Source: Internet
Author: User

Random Class

1. Classes for generating random numbers

If you create two Random instances with the same seed, the same method call sequence is made for each instance, and they will generate and return the same sequence of numbers.

2. Construction Method:

A:random (): Creates a new random number generator. This construction method sets the seed of the random number generator to a value that is completely different from the value that is used by all other invocations of this construction method.

The seed is not given, with the default seed, which is the millisecond value of the current time , different from the random number generated each time

B:random (Long Seed): Creates a new random number generator with a single long seed. The seed is the initial value of the internal state of the pseudo-random number generator, which can be maintained by means of protected int next (int bits).

The seed is specified, and the random number is the same for each seed

3. Member Methods:

A:int Nextint () returns the random number in the INT range

The Random class implements the Nextint method as follows:

public int Nextint () {

Return next (32);

}

B:int nextint (int n) returns the random number in the range of [0,n]

Public class Randomdemo {

Public staticvoid main (string[] args) {

        // Creating Objects

//Random R = new Random (); // no seed given, different random number produced at a time

random R = new random (1111); given seed, the same random number is generated each time

          for ( int x = 0; x < 10; x++) {

             //int num = R.nextint ();//int Span style= "Font-family:consolas" >

intnum = r. Nextint (+ 1); random number in [1,100]

System. out. println (num);

}

}

}

Java_ Common Class 09_random class

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.