Java (Math class and Random class), mathrandom

Source: Internet
Author: User
Tags natural logarithm

Java (Math class and Random class), mathrandom
I. Math Overview

Provides common mathematical calculation methods and two static constants E (base number of natural logarithm) and PI (circumference rate)

Ii. Common Methods
Package com. pb. demo1; public class MathTest {public static void main (String [] args) {System. out. println ("square root:" + Math. sqrt (9.0); System. out. println ("calculate the maximum value of two numbers:" + Math. max (10, 30); System. out. println ("minimum value of two numbers:" + Math. min (10, 30); System. out. println ("2's 3rd power:" + Math. pow (2, 3); System. out. println ("Rounding:" + Math. round (33.6 ));
System. out. println ("random number between 0 and 1:" + Math. random ());}}
3. Random is a Random number generation class. You can specify a Random number range and then generate any number in this range.
No. Method Type Description
1 Public boolean nextBoolean () Normal Generate a boolean value randomly
2 Public double nextDouble () Normal Random double value generation
3 Public float nextFloat () Normal Randomly generate float values
4 Public int nextInt () Normal Generate int value randomly
5 Public int nextInt (int n) Normal Generate the int value of the given maximum value randomly
6 Public long nextLong () Normal Generate a random long value
 
package com.pb.demo1;import java.util.Random;public class RandomTest {    public static void main(String[] args) {        Random random=new Random();        for (int i = 1; i <=10; i++) {            System.out.println(random.nextInt(i));                    }    }}

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.