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)); } }}