Two ways to generate random numbers in Java

Source: Internet
Author: User

"Method One"

Call the Random class in Util: Defines the object of the random rand, generating random integers with rand.nextint ()

or the int after next is changed to Double,float, long, which corresponds to double precision, single precision and long shaping.

Note that only Nextint () can take parameters, for example : Rand.nextint (10) randomly generates random numbers from 0 to 9;

Import Java.util.random;public class Test{public static void Main (string[] args) {random xx=new random (); for (int i=0;i <10;i++) {int number=xx.nextint (10); System.out.println ("Random number is" + number);} Random x2=new random (); for (int i=0;i<10;i++) {int number=x2.nextint (10); System.out.println ("Random number is" + number);}}}

The random number generated by the two random number objects is indeed "random";


Random number generated with a seed for example: The random rand=random (10) seed is 10, and each generation is the same.

You can also use Rand.setseed (n) to reset the seed;

Import Java.util.random;public class test{public static void Main (string[] args) {random r=new random (10);        for (int i=0;i<10;i++) {System.out.println (R.nextint (10));        } System.out.println (); Random r2=new random (10);//seeds are all ten, for (int i=0;i<10;i++) {System.out.println (R2.nextint (10));//Output the same Two sequence}}}

Summarize:

The random object generates random numbers according to the seed , and the seed is the same as the randomly object, whenever it is run, and no matter what time it is on that machine, the generated sequence is the same, and the seed is different. object, the generated sequence of random numbers is not the same.

The sequence of random numbers that are generated by using random constructors with no parameters is the number of milliseconds of the current system time.


"Method Two"

1. The static (static) method in the Math Library random ()

The function of this method is to produce a double value between 0 and 1 (including 0, but not 1).

double rand = Math.random ();




Two ways to generate random numbers 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.