Java starts from scratch 28 (Math class and Random Class)

Source: Internet
Author: User
Tags natural logarithm

First, math overview

Provides common mathematical operations and two static constants E (base of natural logarithm) and pi (π)

Second, common methods
 package   Com.pb.demo1;  public  class   Mathtest { static  void   main (string[] args) {System        . OUT.PRINTLN ( "squared root:" + math.sqrt (9.0 "The maximum value of two:" + Math.max (10,30 "minimum value for two numbers:" + math.min (10,30 2 of 3 times: "+ Math.pow (2,3 rounding: "+ math.round (33.6  System.out.println ("generate a random number between 0-1:" +math.random ()); }}  
Random class randomness is a randomly generated class that can specify a range of random numbers that can then be arbitrarily generated in this range.
No. Method Type Describe
1 public boolean Nextboolean () Ordinary Randomly generate a Boolean value
2 Public double nextdouble () Ordinary Randomly generated double values
3 public float nextfloat () Ordinary Randomly generated float values
4 public int Nextint () Ordinary Randomly generated int values
5 public int Nextint (int n) Ordinary Randomly generates an int value for a given maximum value
6 Public long Nextlong () Ordinary Generate a Long value randomly
 Package Com.pb.demo1; Import Java.util.Random;  Public class randomtest {    publicstaticvoid  main (string[] args) {        Random Random=new  random ();          for (int i = 1; I <=10; i++) {            System.out.println (Random.nextint (i));     }}}

Java starts from scratch 28 (Math class and 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.