Random number Java.util.Random and java.lang.Math.Random ()-java

Source: Internet
Author: User
Tags generator

Random number Java.util.Random and java.lang.Math.Random ()-javaoriginal June 05, 2015 13:24:40 http://blog.csdn.net/Scryhuaihuai/article/details/46375693

First, Java.util.Random
The random class has two construction methods: Random () (using system time as a seed) and random (long seed). The constructor method simply creates a random number generator, and you must call the generator's method to produce a random number. The common methods of random are:
1.NEXTINT (): Returns a pseudo-random number of type int, with the value of the pseudo-random number within the int range.
2.nextInt (int N): Returns a pseudo-random number of type int, the value of the pseudo-random number is between [0,n].

Random random=new Random();int num1=random.nextInt(10);//生成一个[0,10)的随机数int num2=1+random.nextInt(10);//生成一个[1,10]的随机数

3.nextLong (): Returns a long type of pseudo-random number, and the value of the pseudo-random number is within the range of long.
4.nextFloat (): Returns a pseudo-random number of type float, with the value of the pseudo-random number between [0.0f,1.0f].
5.nextDouble (): Returns a pseudo-random number of type double, with the value of the pseudo-random number between [0.0d,1.0d].
6.nextBoolean (): Returns a Boolean pseudo-random number with a value of true or false for pseudo-random numbers.
Second, java.lang.Math.Random ()
It is a method, and Java.util.Random is a class.
public static double Random () returns a double with a positive number that is greater than or equal to 0.0 and less than 1.0. The return value is a pseudo-random number that is evenly distributed within the range (approximate).

double num1=Math.random();     //生成[0.0d,1.0d)的随机数int num2=(int)(Math.random()*10); //生成[0,10)的随机数int num3=(int)(1+Math.random()*10);//生成的[1,10]随机数

Random number Java.util.Random and java.lang.Math.Random ()-java

Related Article

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.