Common Methods for javaRandom classes

Source: Internet
Author: User

The construction method of the Random class:

Public Random (); // This constructor uses a number related to the relative time of the current system time as the number of seeds.

Public Random (long seed); // you can specify the number of seeds to create.



Method:

Public boolean nextBoolean ();

Public double nextDouble (); // generates a random double value, between 0 and 1.0)

Public int nextInt (); // range between int-231-231-1

Public int nextInt (int n); // [0, n)

Public void setSeed (long seed );

Random r = new Random ();

Double d1 = r. nextDouble ();

Double d2 = r. nextDouble () * 5; // [0, 5.0) means to increase the range by 5 times

Double d3 = r. nextDouble () * 1.5 + 1; // [1, 2.5) first generate a random number in the [0, 1.5) interval and Add 1.

Int n1 = nextInt (); // generates any integer

Int n2 = r. nextInt (10); // [0, 10)

N2 = Math. abs (r. nextInt () % 10); // [0, 10)


Int n2 = r. nextInt (n); // [0, n)

N2 = Math. abs (r. nextInt () % n );

Int n3 = r. nextInt (11); // [0, 10]

N3 = Math. abs (r. nextInt () % 11); // [0, 10]

Int n4 = r. nextInt (18)-3; // [-3, 15)

N4 = Math. abs (r. nextInt () % 18)-3;

The following java program demonstrates some basic operations:

Package nemo;

Import java. util .*;

Public class lala {

Public static void main (String [] args)

{

Random r = new Random ();

Int n2 = r. nextInt (10); // [0, 10)

Int n3 = r. nextInt (11); // [0, 10]

Int n1 = r. nextInt (18)-2; // [-2, 16)

System. out. println (n2 + "+ n3 +" "+ n1 );

Boolean b1 = r. nextBoolean ();

Boolean b2 = r. nextBoolean ();

System. out. println (b1 + "" + b2 );

Double d1 = r. nextDouble (); // [0, 1.0)

Double d2 = r. nextDouble () * 5; // [0, 5.0)

Double d3 = r. nextDouble () * 1.5 + 1; // [1, 2.5)

System. out. println (d1 + "" + d2 + "" + d3 );


}

}

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.