Java Api--random Class

Source: Internet
Author: User

1. Random class overviewThis class is used to generate random numbers if two random instances are created 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 MethodPublic random (): Creates a new random number generator, does not give the seed, the default seed is the millisecond value of the current time public random (long Seed): Creates a new random number generator with a single long seed, given the seed, each time the resulting random number is is the same. 3. Random class member Methodpublic int Nextint (): Returns the next pseudo-random number, which is an int value that is evenly distributed in the sequence of this random number generator.         public int Nextint (int n): Returns a pseudo-random number, which is an int value that is evenly distributed between 0 (inclusive) and the specified value (not included) from this random number generator sequence.
ImportJava.util.Random; Public classRandomDemo01 { Public Static voidMain (string[] args) {Random R1=NewRandom ();  for(intx = 0; x < 10; X + +) {            intNUM1 = R1.nextint (100) + 1; System.out.print (NUM1+ ",");        } System.out.println (); System.out.println ("---------------------------"); //given the seed, each time the resulting random number is the same. Random r2 =NewRandom (1111);  for(intx = 0; x < 10; X + +) {            intnum2 = R2.nextint (100) + 1; System.out.print (num2+ ","); }    }}

Output Result:

First time execution:
81,34,92,11,52,1,95,44,7,42,---------------------------27,7,6,70,51,28,26,5,99,17, Second execution:76,80,61,61,29,92,19,16,36,51,---------------------------27,7,6,70,51,28,26,5,99,17,

Java Api--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.