Java's random

Source: Internet
Author: User

The random number of is pseudo-random, and in one interval the probability is generated. Use this to achieve probabilistic problems:
Achieve 60% chance to execute a program, 30% chance to execute B program, 10% chance to execute C program.
Random rnd = new Random ();d ouble d = rnd.nextdouble (); if (d<=0.6) {System.out.println ("execute a");} else if (d<=0.9) {System.out.println ("execute B");} Else{system.out.println ("Execute C");}

The number of seeds in 2.Random is the number of origins that generate random numbers, and is not related to what is generated. The same random number generated by random numbers with the same number of two seeds is the same, and the internal execution algorithm is consistent, and the application typically uses only one random object. However, the number of seeds for new random () is random, and the same random number generated by the two new random () is basically different.
The method in 3.Random nextbytes (byte[] bytes) attaches random bytes to bytes, with no return value.
4. A few examples:
Generate a random number between [ -3,15]:
System.out.println (Rnd.nextint (4) * ( -1) + rnd2.nextint (15)); System.out.println (Rnd.nextint (18)-3);

Generate a random number between [1,2.5]:
SYSTEM.OUT.PRINTLN (1 + rnd.nextdouble () * 0.5 + rnd.nextint (2)); System.out.println (rnd.nextdouble () * 1.5 + 1);

Generated. Random number between [0,5.0]:
System.out.println (rnd2.nextdouble () + rnd2.nextint (5)); System.out.println (Rnd2.nextdouble () * 5);

5.nextBoolean randomly generated true false,math.random () is called by the Random nextdouble ()

Java's random

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: 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.